多執行緒通訊(wait和notify)
阿新 • • 發佈:2018-12-24
import java.util.ArrayList; import java.util.List; public class ListAdd1 { private volatile static List list = new ArrayList(); public void add(){ list.add("beijing"); } 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(); } }
import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; /** * wait notfiy 方法,wait釋放鎖,notfiy不釋放鎖 */ public class ListAdd2 { private volatile static List list = new ArrayList(); public void add(){ list.add("beijing"); } public int size(){ return list.size(); } public static void main(String[] args) { final ListAdd2 list2 = new ListAdd2(); // 1 例項化出來一個 lock // 當使用wait 和 notify 的時候 , 一定要配合著synchronized關鍵字去使用 final Object lock = new Object(); final CountDownLatch countDownLatch = new CountDownLatch(1); Thread t1 = new Thread(new Runnable() { @Override public void run() { try { synchronized (lock) { 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("已經發出通知.."); countDownLatch.countDown(); lock.notify(); } } } } catch (InterruptedException e) { e.printStackTrace(); } } }, "t1"); Thread t2 = new Thread(new Runnable() { @Override public void run() { synchronized (lock) { if(list2.size() != 5){ try { System.out.println("t2進入..."); lock.wait(); //countDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("當前執行緒:" + Thread.currentThread().getName() + "收到通知執行緒停止.."); throw new RuntimeException(); } } }, "t2"); t2.start(); t1.start(); } }
/** * wait notfiy 方法,wait釋放鎖,notfiy不釋放鎖 */ public class ListAdd2 { private volatile static List list = new ArrayList(); public void add(){ list.add("beijing"); } public int size(){ return list.size(); } public static void main(String[] args) { final ListAdd2 list2 = new ListAdd2(); // 1 例項化出來一個 lock // 當使用wait 和 notify 的時候 , 一定要配合著synchronized關鍵字去使用 //final Object lock = new Object(); final CountDownLatch countDownLatch = new CountDownLatch(1); Thread t1 = new Thread(new Runnable() { @Override public void run() { try { // synchronized (lock) { 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("已經發出通知.."); countDownLatch.countDown(); // lock.notify(); } } // } } catch (InterruptedException e) { e.printStackTrace(); } } }, "t1"); Thread t2 = new Thread(new Runnable() { @Override public void run() { // synchronized (lock) { if(list2.size() != 5){ try { System.out.println("t2進入..."); // lock.wait(); countDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("當前執行緒:" + Thread.currentThread().getName() + "收到通知執行緒停止.."); throw new RuntimeException(); } // } }, "t2"); t2.start(); t1.start(); } }
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
/**
* wait notfiy 方法,wait釋放鎖,notfiy不釋放鎖
*/
public class ListAdd2 {
private volatile static List list = new ArrayList();
public void add(){
list.add("beijing");
}
public int size(){
return list.size();
}
public static void main(String[] args) {
final ListAdd2 list2 = new ListAdd2();
// 1 例項化出來一個 lock
// 當使用wait 和 notify 的時候 , 一定要配合著synchronized關鍵字去使用
//final Object lock = new Object();
final CountDownLatch countDownLatch = new CountDownLatch(2);
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
// synchronized (lock) {
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("已經發出通知..");
countDownLatch.countDown();
countDownLatch.countDown();
// lock.notify();
}
}
// }
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
// synchronized (lock) {
if(list2.size() != 5){
try {
System.out.println("t2進入...");
// lock.wait();
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("當前執行緒:" + Thread.currentThread().getName() + "收到通知執行緒停止..");
throw new RuntimeException();
}
// }
}, "t2");
t2.start();
t1.start();
}
}