通信
1. 等待
在同步代碼中調用鎖對象的wait()方法,可以讓當前線程等待
2. 通知喚醒
使用鎖對象的notify()方法可以喚醒在該對象上等待的隨機一個線程
使用鎖對象的notifyAll()方法可以喚醒在該對象上等待的所有線程
示例:
創建三個線程,其中一個線程內部執行3次打印,第二個線程內部執行5次打印,第三個線程內部執行7次打印,如此交替執行10次。
public class NotifyTest {
public static void main(String[] args) {
final NotifyService service = new NotifyService();
//線程一
new Thread(new Runnable() {
public void run() {
for (int j = 0; j < 10; j++)
service.print1(j);
}
}).start();
//線程二
new Thread(new Runnable() {
public void run() {
for (int j = 0; j < 10; j++)
service.print2(j);
}
}).start();
//線程三
new Thread(new Runnable() {
public void run() {
for (int j = 0; j < 10; j++)
service.print3(j);
}
}).start();
}
}
class NotifyService {
private int flag = 1; // 當前輪到哪個
public synchronized void print1(int j) {
while(flag != 1){
try {
this.wait(); // 如果不該1, 就等待
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 0; i < 3; i++){
System.out.println("線程1: " + j);
}
System.out.println();
flag = 2; // 1結束後輪到2
this.notifyAll(); // notify()喚醒隨機一個, notifyAll()喚醒所有
}
public synchronized void print2(int j) {
while(flag != 2){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 0; i < 5; i++){
System.out.println("線程2: " + j);
}
System.out.println();
flag = 3;
this.notifyAll();
}
public synchronized void print3(int j) {
while(flag != 3){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 0; i < 7; i++){
System.out.println("線程3: " + j);
}
System.out.println();
flag = 1;
this.notifyAll();
}
}
線程範圍內共享數據
1.應用場景
在程序開發過程中我們經常需要在同一個線程中共享數據,例如我們常見的銀行轉賬的案例,轉入和轉出是同一個線程上執行的兩個方法,他們應該共享一個事務對象。
1) 自定義Map
使用一個Key對象爲Thread類型的Map用來保存數據。
在存儲對象時將當前線程存爲Key,數據存爲Value。獲取對象時使用當前線程對象即可獲取到線程內部共享的數據。
採用Map來存儲可能導致內存溢出
2) ThreadLocal
Java中爲我們提供了一個和當前線程相關的容器ThreadLocal,實際上內部就是使用Map來進行存儲的,只不過做了優化,在線程結束時會自動清空Map中的數據,以防止內存溢出。
當調用其set()方法時會將數據和當前線程綁定,調用get()方法時則是獲取和當前線程綁定的數據。
一個ThreadLocal只能存儲一個數據,如果有多個數據需要在線程範圍內共享,可以創建多個ThreadLocal,或者將多個數據存入一個對象,將對象存入ThreadLocal。
使用Map在線程範圍內共享數據:
private static Map<Thread, Integer> map = new HashMap<Thread, Integer>();
public static void main(String[] args) {
new Thread(new Runnable() {
public void run() {
map.put(Thread.currentThread(), 5);
System.out.println(Thread.currentThread().getName() + " set Data: " + 5);
new A().get();
new B().get();
}
}).start();
new Thread(new Runnable() {
public void run() {
map.put(Thread.currentThread(), 10);
System.out.println(Thread.currentThread().getName() + " set Data: " + 10);
new A().get();
new B().get();
}
}).start();
}
private static class A {
public void get() {
System.out.println(Thread.currentThread().getName() + " A.get Data: " + map.get(Thread.currentThread()));
}
}
private static class B {
public void get() {
System.out.println(Thread.currentThread().getName() + " B.get Data: " + map.get(Thread.currentThread()));
}
}
使用ThreadLocal在線程範圍內共享數據: // key值固定爲Thread的一個Map容器, 存儲對象的時候, 用當前線程作爲key, 獲取的時候也是當前線程作爲key
// 在線程銷燬的時候, 容器中的記錄會自動刪除
private static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>();
public static void main(String[] args) {
new Thread(new Runnable() {
public void run() {
threadLocal.set(5); // 用當前線程作爲key存儲數據
System.out.println(Thread.currentThread().getName() + " set Data: " + 5);
new A().get();
new B().get();
}
}).start();
new Thread(new Runnable() {
public void run() {
threadLocal.set(10);
System.out.println(Thread.currentThread().getName() + " set Data: " + 10);
new A().get();
new B().get();
}
}).start();
}
private static class A {
public void get() {
// 用當前線程作爲key獲取數據
System.out.println(Thread.currentThread().getName() + " A.get Data: " + threadLocal.get());
}
}
private static class B {
public void get() {
System.out.println(Thread.currentThread().getName() + " B.get Data: " + threadLocal.get());
}
}
使用ThreadLocal在線程範圍內共享多個數據: public static void main(String[] args) {
new Thread(new Runnable() {
public void run() {
SharedData data = SharedData.getSharedData(); // 獲取當前線程的共享數據
data.setName("張三");
data.setAge(19);
System.out.println(Thread.currentThread().getName() + " set Data: " + data);
new A().get();
new B().get();
}
}).start();
new Thread(new Runnable() {
public void run() {
SharedData data = SharedData.getSharedData();
data.setName("李四");
data.setAge(20);
System.out.println(Thread.currentThread().getName() + " set Data: " + data);
new A().get();
new B().get();
}
}).start();
}
private static class A {
public void get() {
System.out.println(Thread.currentThread().getName() + " A.get Data: " + SharedData.getSharedData());
}
}
private static class B {
public void get() {
System.out.println(Thread.currentThread().getName() + " B.get Data: " + SharedData.getSharedData());
}
}
}
class SharedData {
// 該類所有實例共享
private static ThreadLocal<SharedData> threadLocal = new ThreadLocal<SharedData>();
// 數據
private String name;
private int age;
private SharedData(){
}
public static SharedData getSharedData(){
SharedData data = threadLocal.get();
if(data == null){
data = new SharedData();
threadLocal.set(data);
}
return data;
}
public String toString() {
return "SharedData(" + name + ", " + age + ")";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}