标签:wait/notify
程序如下:
public static void main(String[] args) throws Exception{
final List list = new ArrayList();
final Object lock = new Object();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock){
for(int i = 0 ; i < 10 ; i++){
list.add(i);
if(list.size() == 5){
lock.notify();
System.out.println(Thread.currentThread().getName() + "发出通知!");
}
}
}
System.out.println(Thread.currentThread().getName() + "execute over!");
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock){
if(list.size() != 5){
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + " 收到通知!");
}
System.out.println(Thread.currentThread().getName() + "execute over!");
}
});
t2.start();
Thread.sleep(1000);
t1.start();
}分析:
程序的意图本是利用多线程之间的通信,利用wait/notify实现,可是运行的结果是虽然线程T1发出了通知,但是线程T2并没有立即收到通知进行执行,这是为什么呢? 因为只有线程T1执行完毕释放了锁,T2才能执行,那么也就是说wait/notify并不是实时的(wait释放了锁,而notify没有释放锁导致的),那么线程之间实时的通信该怎么做呢?可以利用CountDownLatch来实现。
对程序的改进:
public static void main(String[] args) throws Exception{
final List list = new ArrayList();
final Object lock = new Object();
final CountDownLatch countDownLatch = new CountDownLatch(1);
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0 ; i < 10 ; i++){
list.add(i);
if(list.size() == 5){
countDownLatch.countDown();
System.out.println(Thread.currentThread().getName() + "发出通知!");
}
}
System.out.println(Thread.currentThread().getName() + "execute over!");
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
if(list.size() != 5){
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " 收到通知!");
}
System.out.println(Thread.currentThread().getName() + "execute over!");
}
});
t2.start();
Thread.sleep(1000);
t1.start();
}本文出自 “学海无涯 心境无限” 博客,请务必保留此出处http://zhangfengzhe.blog.51cto.com/8855103/1875221
标签:wait/notify
原文地址:http://zhangfengzhe.blog.51cto.com/8855103/1875221