您的位置:首页 > 编程语言 > Java开发

Java总结之线程

2014-10-18 16:11 267 查看
【线程的基本概念】

线程是一个程序内部的顺序控制流。

线程和进程的区别:

每个进程都有独立的代码和数据空间(进程上下文),进程间的切换会有较大的开销。

线程可以看成是轻量级的进程,同一类线程共享代码和数据空间,每个县城有独立的运行站和程序计数器(PC),线程切换的开销小。

多进程:在操作系统中能同时运行多个任务(程序)

多线程:在同一应用程序中有多个顺序流同时执行

Java的线程是通过java.lang.Thread类来实现的。

VM启动时会有一个由主方法(public static void main(){})所定义的线程。

可以通过创建Thread的实例来创建新的线程。

每个线程都是通过摸个特定Thread对象所对应的方法run()来完成其操作的,方法run()称为线程体。

通过调用Thread类的start()方法来启动一个线程。

【线程控制基本方法】

isAlive() 判断线程是否还“活”着,即线程是否还未终止。

getPriority() 获得线程的优先级数值

setPriority() 设置现成的优先级数值

Thread.sleep() 将当前线程睡眠指定毫秒数

join() 调用某线程的该方法,将当前线程与该线程“合并”,即等待该线程结束,再恢复当前线程的运行。

yield() 让出CPU,当前线程进入就绪队列等待调度。

wait() 当前线程进入对象的wait pool。

notify()/notifyAll() 唤醒对象的wait pool中的一个/所有等待线程。

【sleep/join/yield方法】

sleep方法

可以调用Thread的静态方法:

public static void sleep(long millis) throws InterruptedExcepion

使得当前线程休眠(暂时停止执行millis毫秒)

由于是静态方法,sleep可以由类名直接调用:

Thread.sleep(...)

join方法

合并某个线程

yield方法

让出CPU,给其他线程执行的机会

join范例:

public class TestJoin extends Thread{
public static void main(String[] args) {
MyThread t1 = new MyThread("t1");
t1.start();
try {t1.join();}
catch (InterruptedException e) {}
for(int i=1;i<=10;i++) {
System.out.println("i am main thread");
}
}
}
class MyThread extends Thread {
MyThread(String s) {super(s);}
public void run() {
for(int i=1;i<=10;i++) {
System.out.println("i am "+getName());
try {sleep(1000);}
catch (InterruptedException e) {return;}
}
}
}


yield范例:

public class TestYield {
public static void main(String[] args) {
MyThread t1 = new MyThread("t1");
MyThread t2 = new MyThread("t2");
t1.start(); t2.start();
}
}
class MyThread extends Thread {
MyThread(String s) {super(s);}
public void run() {
for(int i=1;i<=100;i++) {
System.out.println(getName()+": "+i);
if(i%10==0) {
yield();
}
}
}
}


【线程的优先级别】

Java提供一个线程调度器来监控程序中启动后进入就绪状态的所有线程。线程调度器按照线程的优先级决定应调度那个线程来执行。

线程的优先级用数字表示,范围从1到10,一个线程的缺省优先级是5.

Thread.MIN_PRIORITY = 1

Thread.MAX_PRIORITY = 10

Thread.NORM_PRIORITY = 5

使用下述方法获得或设置线程对象的优先级。

int getPriority();

void setPriority(int new Priority);

Priority范例:

public class TestPriority {
public static void main(String[] args) {
Thread t1 = new Thread(new T1());
Thread t2 = new Thread(new T2());
t1.setPriority(Thread.NORM_PRIORITY + 3);
t1.start();
t2.start();
}
}
class T1 implements Runnable {
public void run() {
for(int i=0;i<100;i++) {
System.out.println("T1: "+i);
}
}
}
class T2 implements Runnable {
public void run() {
for(int i=0;i<100;i++) {
System.out.println("------T2: "+i);
}
}
}


【线程同步】

在Java语言中,引入了对象互斥锁的概念,保证共享数据操作的完整性。每个对象都对应于一个可称为"互斥锁"的标记,这个标记保证在任意时刻,只能有一个线程访问该对象。

关键字synchronized来与对象互斥锁联系。当某个对象synchronized修饰时,表明该对象在任意时刻只能由一个线程访问。

synchronized 的使用方法:

synchronized(this) {...}

synchronized还可以放在方法声明中,表示整个方法为同步方法,例如:

synchronized public void add(String name) {...}

范例:

public class TestSync implements Runnable {
Timer timer = new Timer();
public static void main(String[] args) {
TestSync test = new TestSync();
Thread t1 = new Thread(test);
Thread t2 = new Thread(test);
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
public void run() {
timer.add(Thread.currentThread().getName());
}
}
class Timer {
private static int num = 0;
public synchronized void add(String name) {
//synchronized (this) {
num ++;
try {Thread.sleep(1);}
catch (InterruptedException e) {}
System.out.println(name+", 你是第"+num+"个使用timer的线程");
//}
}
}


【死锁】

范例:

public class TestDeadLock implements Runnable {
public int flag = 1;
static Object o1 = new Object(), o2 = new Object();
public void run() {
System.out.println("flag=" + flag);
if(flag == 1) {
synchronized(o1) {
try {Thread.sleep(500);}
catch (Exception e) {
e.printStackTrace();
}
synchronized(o2) {
System.out.println("1");
}
}
}
if(flag == 0) {
synchronized(o2) {
try {Thread.sleep(500);}
catch (Exception e) {
e.printStackTrace();
}
synchronized(o1) {
System.out.println("0");
}
}
}
}
public static void main(String[] args) {
TestDeadLock td1 = new TestDeadLock();
TestDeadLock td2 = new TestDeadLock();
td1.flag = 1;
td2.flag = 0;
Thread t1 = new Thread(td1);
Thread t2 = new Thread(td2);
t1.start();
t2.start();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: