package com.ietree.multithread.sync;
import java.util.ArrayList;
import java.util.List;
public class MyList {
private volatile static List list = new ArrayList();
public void add() {
list.add("apple");
}
public int size() {
return list.size();
}
public static void main(String[] args) {
final MyList list1 = new MyList();
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添加了一個元素..
當前線程:t1添加了一個元素..
Exception in thread "t2" 當前線程收到通知:t2 list size = 5 線程停止..
java.lang.RuntimeException
at com.ietree.multithread.sync.MyList$2.run(MyList.java:43)
at java.lang.Thread.run(Unknown Source)
當前線程:t1添加了一個元素..
當前線程:t1添加了一個元素..
當前線程:t1添加了一個元素..
當前線程:t1添加了一個元素..
當前線程:t1添加了一個元素..
理解:線程Thread2不停地通過while語句檢測這個條件(list.size()==5)是否成立 ,從而實現了線程間的通信。但是這種方式會浪費CPU資源。
使用這種方式實現線程通信需要注意:wait和notify必須配合synchronized關鍵字使用,wait方法釋放鎖,notify方法不釋放鎖。並且在這個例子中必須是Thread2先執行才可以。
package com.ietree.multithread.sync;
import java.util.ArrayList;
import java.util.List;
public class ListAdd3 {
private volatile static List list = new ArrayList();
public void add() {
list.add("apple");
}
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();
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("已經發出通知..");
//不釋放鎖,遇到size=5時還是繼續執行
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 {
//釋放鎖,讓其他線程執行
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("當前線程:" + Thread.currentThread().getName() + "收到通知線程停止..");
throw new RuntimeException();
}
}
}, "t2");
t2.start();
t1.start();
}
}
程序輸出:
當前線程:t1添加了一個元素..
當前線程:t1添加了一個元素..
當前線程:t1添加了一個元素..
當前線程:t1添加了一個元素..
當前線程:t1添加了一個元素..
已經發出通知..
當前線程:t1添加了一個元素..
當前線程:t1添加了一個元素..
當前線程:t1添加了一個元素..
當前線程:t1添加了一個元素..
當前線程:t1添加了一個元素..
當前線程:t2收到通知線程停止..
Exception in thread "t2" java.lang.RuntimeException
at com.ietree.multithread.sync.ListAdd3$2.run(ListAdd3.java:59)
at java.lang.Thread.run(Unknown Source)
package com.ietree.multithread.sync;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
public class ListAdd2 {
private volatile static List list = new ArrayList();
public void add() {
list.add("apple");
}
public int size() {
return list.size();
}
public static void main(String[] args) {
final ListAdd2 list2 = new ListAdd2();
final CountDownLatch countDownLatch = new CountDownLatch(1);
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
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();
}
}
// }
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
if (list2.size() != 5) {
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("當前線程:" + Thread.currentThread().getName() + "收到通知線程停止..");
throw new RuntimeException();
}
}, "t2");
t2.start();
t1.start();
}
}
程序輸出:
當前線程:t1添加了一個元素..
當前線程:t1添加了一個元素..
當前線程:t1添加了一個元素..
當前線程:t1添加了一個元素..
當前線程:t1添加了一個元素..
已經發出通知..
Exception in thread "t2" 當前線程:t1添加了一個元素..
當前線程:t2收到通知線程停止..
java.lang.RuntimeException
at com.ietree.multithread.sync.ListAdd2$2.run(ListAdd2.java:56)
at java.lang.Thread.run(Unknown Source)
當前線程:t1添加了一個元素..
當前線程:t1添加了一個元素..
當前線程:t1添加了一個元素..
當前線程:t1添加了一個元素..