小伙伴們好呀,最近在重新復習,整理自己的知識庫,偶然看到這道面試題:三個線程按順序打印 ABCABC,嘗試著做一下,才發現自己對線程還有好多地方不懂,藍瘦…… ??
思路
很明顯,這里就涉及線程間相互通信的知識了。
而相互通信的難點就是要控制好,阻塞和喚醒的時機。
一. 這里就是 A 通知 B,B 通知 C , C 通知 A

二. 三個線程在等待(阻塞)和喚醒(執行) 中不斷切換。
三. 等待的方式大致分為兩種
- wait 方法 (Object native 方式 )
- LockSupport.park 方式 ( Unsafe native 方式 )
四. 喚醒的方式
- notify,notifyAll 方法 (Object native 方式 )
- LockSupport.unPark 方式 ( Unsafe native 方式 )
五. 互斥條件
線程 A 先拿到資源 c,再拿資源 a ,[a 執行完后釋放,并喚醒等待資源 a] 的 線程 B 線程 B 先拿到資源 a,再拿資源 b ,[b 執行完后釋放,并喚醒等待資源 b] 的 線程 C 線程 C 先拿到資源 b,再拿資源 c ,[c 執行完后釋放,并喚醒等待資源 c] 的 線程 A
所以得有 三個 共享資源 abc 來達到互斥條件
Synchronized 還是 ReentrantLock 都得建立 三個共享資源

六. 擴展
使用 LockSupport ,如果要像上面這樣子的思路去解答,就得注意 線程相互引用行成的循環依賴問題,這里借用 Spring 的思路 用 Map 巧妙化解。
或者做法2 通過 外部的成員變量,不斷地去判斷,unpark 線程 a b c
Synchronized 方式
private static class MySynchronized {
void printABC() throws InterruptedException {
class MyRunable implements Runnable {
private Object lock1;
private Object lock2;
private CountDownLatch countDownLatch;
public MyRunable(Object lock1, Object lock2){
this.lock1 = lock1;
this.lock2 = lock2;
}
public MyRunable(Object lock1, Object lock2, CountDownLatch countDownLatch){
this.lock1 = lock1;
this.lock2 = lock2;
this.countDownLatch = countDownLatch;
}
@Override
public void run(){
boolean running = false;
int count = 2;
while (count > 0) {
// C,A - > A 喚醒 B 線程
// A,B - > B 喚醒 C 線程
// B,C - > C 喚醒 A 線程 (最后一次執行時,喚醒 A 后,A 發現 count =0,就不執行了。
synchronized (lock1) {
synchronized (lock2) {
System.out.println(Thread.currentThread().getName());
count--;
// lock2 方法塊執行結束前,喚醒其他線程。
lock2.notify();
}
// 線程執行完畢后
if (countDownLatch != null && !running) {
countDownLatch.countDown();
running = true;
}
try {
// 釋放鎖
lock1.wait();
} catch (InterruptedException e) {
}
}
}
System.out.println(Thread.currentThread().getName() + " over");
synchronized (lock2) {
// 喚醒其他線程。
lock2.notify();
}
}
}
CountDownLatch countDownLatch = new CountDownLatch(1);
CountDownLatch countDownLatch2 = new CountDownLatch(1);
Object a = new Object();
Object b = new Object();
Object c = new Object();
MyRunable ra = new MyRunable(c, a, countDownLatch);
MyRunable rb = new MyRunable(a, b, countDownLatch2);
MyRunable rc = new MyRunable(b, c);
Thread a1 = new Thread(ra, "A");
a1.start();
countDownLatch.await();
Thread b1 = new Thread(rb, "B");
b1.start();
countDownLatch2.await();
Thread c1 = new Thread(rc, "C");
c1.start();
}
}
這里我借用 countDownLatch 去控制線程的啟動流程,盡量不使用 Thread.sleep() 來實現,拿捏線程的執行,通信步驟。
寫這個的時候,除了一開始思路不清晰外,還出現一個小狀況,就是 程序執行完卡住了。

debug 發現線程 B C 還在 wait 狀態,這是寫時候容易疏忽的。
要記得在循環外再次喚醒其他線程,讓他們走完方法。

ReentrantLock 方式
private static class MyReentrantLock {
int number = 6;
void printABC(){
ReentrantLock lock = new ReentrantLock();
Condition conditionA = lock.newCondition();
Condition conditionB = lock.newCondition();
Condition conditionC = lock.newCondition();
class MyRunnable implements Runnable {
ReentrantLock lock;
Condition condition1;
Condition condition2;
public MyRunnable(ReentrantLock lock, Condition condition1, Condition condition2){
this.lock = lock;
this.condition1 = condition1;
this.condition2 = condition2;
}
@Override
public void run(){
int count = 2;
while (count > 0) {
lock.lock();
try {
String name = Thread.currentThread().getName();
if (
number % 3 != 0 && "A".equals(name)
|| number % 3 != 2 && "B".equals(name)
|| number % 3 != 1 && "C".equals(name)
) {
condition1.await();
}
System.out.println(name + " : " + number);
number--;
count--;
condition2.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
}
new Thread(new MyRunnable(lock, conditionC, conditionA), "A").start();
new Thread(new MyRunnable(lock, conditionA, conditionB), "B").start();
new Thread(new MyRunnable(lock, conditionB, conditionC), "C").start();
}
}
Synchronized 會了之后,這個也很簡單了。
就是上鎖的地方換成 lock.lock();,把三個共享資源換成 lock.newCondition();
然后思考一下阻塞條件 condition1.await() 。
畢竟 打印 和 喚醒 的操作總是在一起的。

Semaphore 我也寫了,但是感覺不太適合,畢竟它的作用是用來控制并發線程數的,我直接創建三個 Semaphore 總覺得怪怪的。??
LockSupport 方式
這里我寫了兩種方法
private static class MyLockSupport {
volatile int number = 6;
void printABC() throws InterruptedException {
class MyRunnable implements Runnable {
@Override
public void run(){
int count = 2;
while (count > 0) {
LockSupport.park(this);
System.out.println(Thread.currentThread().getName());
count--;
}
}
}
Thread a = new Thread(new MyRunnable(), "A");
Thread b = new Thread(new MyRunnable(), "B");
Thread c = new Thread(new MyRunnable(), "C");
a.start();
b.start();
c.start();
while (number > 0) {
if (number % 3 == 0) {
LockSupport.unpark(a);
} else if (number % 3 == 2) {
LockSupport.unpark(b);
} else {
LockSupport.unpark(c);
}
number--;
LockSupport.parkNanos(this, 200 * 1000);
// LockSupport.parkUntil(this,System.currentTimeMillis()+3000L);
}
}
// 用 map 解決線程循環依賴的問題
void printABC2() throws InterruptedException {
class MyRunnable implements Runnable {
Map<String, Thread> map;
public MyRunnable(Map<String, Thread> map){
this.map = map;
}
@Override
public void run(){
int count = 2;
String name = Thread.currentThread().getName();
String key = "A".equals(name) ? "B" : "B".equals(name) ? "C" : "A";
while (count > 0) {
if (
number % 3 == 0 && "A".equals(name)
|| number % 3 == 2 && "B".equals(name)
|| number % 3 == 1 && "C".equals(name)
) {
System.out.println(name);
count--;
number--;
LockSupport.unpark(map.get(key));
}
LockSupport.park(this);
}
LockSupport.unpark(map.get(key));
}
}
Map<String, Thread> map = new HashMap<>();
Thread a = new Thread(new MyRunnable(map), "A");
Thread b = new Thread(new MyRunnable(map), "B");
Thread c = new Thread(new MyRunnable(map), "C");
map.put("A", a);
map.put("B", b);
map.put("C", c);
a.start();
b.start();
c.start();
}
}
LockSupport 我也是第一次用,它使用起來也很方便,就單純的 阻塞和喚醒線程 ,對應 park 和 unPark 方法。
它不要求你像 wait 那樣子,必須寫在 Synchronized 代碼塊里,被 Monitor 監視才行。
但同時,也意味著你必須控制好這個 鎖的范圍 。
你可以自由阻塞代碼,在具備某個條件時,喚醒特定的線程,讓它繼續執行。
實際上,上面 ReentrantLock 中的 Condition await 方法,底層就是調用 LockSupport 的 park 方法。
這也是我開頭說的通信大致分為兩種方式的原因。
方法一中,我是用 parkNanos 阻塞一段時間,然后就繼續運行,也算是取巧不用 Thread.Sleep 了吧??
方法二 我比較喜歡,思路也是同開頭兩種,打印完喚醒其他線程。