JAVA執行緒中的關於wait和notify不錯的例子
JAVA執行緒中的關於wait和notify不錯的例子,原來的實現方法:
public class ListAdd1 { private volatile static List list = new ArrayList(); public void add(){ list.add("bjsxt"); } public int size(){ return list.size(); } public static void main(String[] args) { final ListAdd1 list1 = new ListAdd1(); Thread t1 = new Thread(new Runnable() { @Override public void run() { try { for(int i = 0; i <10; i++){ list1.add(); System.out.println("當前執行緒:" + Thread.currentThread().getName() + "添加了一個元素.."); Thread.sleep(500); } } catch (InterruptedException e) { e.printStackTrace(); } } }, "t1"); Thread t2 = new Thread(new Runnable() { @Override public void run() { while(true){ if(list1.size() == 5){ System.out.println("當前執行緒收到通知:" + Thread.currentThread().getName() + " list size = 5 執行緒停止.."); throw new RuntimeException(); } } } }, "t2"); t1.start(); t2.start(); }
是兩個執行緒互相通訊的,輸出:
當前執行緒:t1添加了一個元素..
當前執行緒:t1添加了一個元素..
當前執行緒:t1添加了一個元素..
當前執行緒:t1添加了一個元素..
Exception in thread "t2" 當前執行緒收到通知:t2 list size = 5 執行緒停止..
java.lang.RuntimeException
at com.bjsxt.base.conn008.ListAdd1$2.run(ListAdd1.java:42)
at java.lang.Thread.run(Thread.java:745)
當前執行緒:t1添加了一個元素..
當前執行緒:t1添加了一個元素..
當前執行緒:t1添加了一個元素..
當前執行緒:t1添加了一個元素..
當前執行緒:t1添加了一個元素..
改為wait,notify的方式:
public class ListAdd2 { private volatile static List list = new ArrayList(); public void add(){ list.add("bjsxt"); } public int size(){ return list.size(); } public static void main(String[] args) { final ListAdd2 list2 = new ListAdd2(); final Object lock = new Object(); Thread t1 = new Thread(new Runnable() { @Override public void run() { try { synchronized (lock) { System.out.println("t1啟動.."); for(int i = 0; i <10; i++){ list2.add(); System.out.println("當前執行緒:" + Thread.currentThread().getName() + "添加了一個元素.."); Thread.sleep(500); if(list2.size() == 5){ System.out.println("已經發出通知.."); lock.notify(); } } } } catch (InterruptedException e) { e.printStackTrace(); } } }, "t1"); Thread t2 = new Thread(new Runnable() { @Override public void run() { synchronized (lock) { System.out.println("t2啟動.."); if(list2.size() != 5){ try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("當前執行緒:" + Thread.currentThread().getName() + "收到通知執行緒停止.."); throw new RuntimeException(); } } }, "t2"); t2.start(); t1.start(); } }
注意一點是:wait是釋放鎖的,但notify僅僅是通知,不釋放鎖。所以這裡是先啟動了t2執行緒,因為一開始
不等於5,所以給T1執行緒去跑,T1跑到第5的時候,NOTIFY T2了,但不釋放鎖,所以繼續執行到10次了,可以看到,這個
不大實時了。可以改用countdownlatch.
public class ListAdd2 {
private volatile static List list = new ArrayList();
public void add(){
list.add("bjsxt");
}
public int size(){
return list.size();
}
public static void main(String[] args) {
final ListAdd2 list2 = new ListAdd2();
//final Object lock = new Object();
final CountDownLatch countDownLatch=new CountDownLatch(1);
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
//synchronized (lock) {
System.out.println("t1啟動..");
for(int i = 0; i <10; i++){
list2.add();
System.out.println("當前執行緒:" + Thread.currentThread().getName() + "添加了一個元素..");
Thread.sleep(500);
if(list2.size() == 5){
System.out.println("已經發出通知..");
// lock.notify();
countDownLatch.countDown();
}
}
//}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
//synchronized (lock) {
System.out.println("t2啟動..");
if(list2.size() != 5){
try {
//lock.wait();
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("當前執行緒:" + Thread.currentThread().getName() + "收到通知執行緒停止..");
throw new RuntimeException();
}
//}
}, "t2");
t2.start();
t1.start();
}
}
輸出就實時了,T2時馬上收到通知停止丟擲異常了,T1繼續執行:
t1啟動..
t2啟動..
當前執行緒:t1添加了一個元素..
當前執行緒:t1添加了一個元素..
當前執行緒:t1添加了一個元素..
當前執行緒:t1添加了一個元素..
當前執行緒:t1添加了一個元素..
已經發出通知..
當前執行緒:t1添加了一個元素..
當前執行緒:t2收到通知執行緒停止..
Exception in thread "t2" java.lang.RuntimeException
at com.bjsxt.base.conn008.ListAdd2$2.run(ListAdd2.java:65)
at java.lang.Thread.run(Thread.java:745)
當前執行緒:t1添加了一個元素..
當前執行緒:t1添加了一個元素..
當前執行緒:t1添加了一個元素..
當前執行緒:t1添加了一個元素..