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

Java多线程之线程间协作 notify与wait的使用

2014-02-13 09:01 239 查看

(转载请注明出处:http://blog.csdn.net/buptgshengod

1.背景

       Java多线程操作运用很广,特别是在android程序方面。线程异步协作是多线程操作的难点也是关键,也是找工作面试经常考到的地方。下面分享一下我的使用心得。

介绍几个关键字:

synchronized:线程锁,使得系统只执行当前线程。

notifyAll():唤醒其它被锁住的线程

wait():挂起线程

ExecutorService exec=Executors.newCachedThreadPool();:创建线程池

exec.execute( new Runnable() ):将线程放到线程池中管理

2.代码部分

(1)

public class Thread2 {
public void m4t1() {
synchronized(this) {
int i = 5;
while(i>0) {
System.out.println("1");
i--;

try {
Thread.sleep(500);

} catch (InterruptedException ie) {
}
}
}
}
public void m4t2() {
// synchronized(this){
int i = 5;
while( i > 0) {
System.out.println("2");
i--;
try {
Thread.sleep(500);

} catch (InterruptedException ie) {
}
}

}
public static void main(String[] args) {
final Thread2 myt2 = new Thread2();
Thread t1 = new Thread( new Runnable() { public void run() { myt2.m4t1(); } }, "t1" );
Thread t2 = new Thread( new Runnable() { public void run() { myt2.m4t2(); } }, "t2" );
t1.start();
t2.start();

}
}

我们发现,两个线程其中一个上了锁,另一个没有上锁。运行结果如下。说明了虽然一个线程上了锁,但是还是能被让其它未上锁线程访问。



(2)

当我们给两个方法都加上锁

public class Thread2 {
public void m4t1() {
synchronized(this) {
int i = 5;
while(i>0) {
System.out.println("1");
i--;

try {
Thread.sleep(500);

} catch (InterruptedException ie) {
}
}
}
}
public void m4t2() {
synchronized(this){
int i = 5;
while( i > 0) {
System.out.println("2");
i--;
try {
Thread.sleep(500);

} catch (InterruptedException ie) {
}
}
}

}
public static void main(String[] args) {
final Thread2 myt2 = new Thread2();
Thread t1 = new Thread( new Runnable() { public void run() { myt2.m4t1(); } }, "t1" );
Thread t2 = new Thread( new Runnable() { public void run() { myt2.m4t2(); } }, "t2" );
t1.start();
t2.start();

}
}运行结果变成了


先执行完第一个锁中的内容后执行第二个锁中的内容。

(3)

这时候我们使用notifyAll()和wait()

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Thread2 {
public void m4t1() {
synchronized(this) {
int i = 5;
while(i>0) {
System.out.println("1");
i--;

try {
Thread.sleep(500);
notifyAll();
wait();
} catch (InterruptedException ie) {
}
}
}
}
public void m4t2() {
synchronized(this){
int i = 5;
while( i > 0) {
System.out.println("2");
i--;
try {
Thread.sleep(500);
notifyAll();
wait();
} catch (InterruptedException ie) {
}
}
}

}
public static void main(String[] args) {
final Thread2 myt2 = new Thread2();
Thread t1 = new Thread( new Runnable() { public void run() { myt2.m4t1(); } }, "t1" );
Thread t2 = new Thread( new Runnable() { public void run() { myt2.m4t2(); } }, "t2" );
t1.start();
t2.start();

}
}

通过不断的唤醒再挂起,两个线程又交替运行。




(4)

如果想让代码更完善,可以将两个线程放到线程池

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Thread2 {
public void m4t1() {
synchronized(this) {
int i = 5;
while(i>0) {
System.out.println("1");
i--;

try {
Thread.sleep(500);
notifyAll();
wait();
} catch (InterruptedException ie) {
}
}
}
}
public void m4t2() {
synchronized(this){
int i = 5;
while( i > 0) {
System.out.println("2");
i--;
try {
Thread.sleep(500);
notifyAll();
wait();
} catch (InterruptedException ie) {
}
}
}

}
public static void main(String[] args) {
final Thread2 myt2 = new Thread2();
ExecutorService exec=Executors.newCachedThreadPool();
exec.execute( new Runnable() { public void run() { myt2.m4t1(); } });
exec.execute( new Runnable() { public void run() { myt2.m4t2(); } });
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: