農(nóng)行一面:如何保證線程T1,T2,T3 順序執(zhí)行?
線程是 Java執(zhí)行的最小單元,通常意義上來說,多個(gè)線程是為了加快速度且無需保序,這篇文章,我們來分析一道農(nóng)業(yè)銀行的面試題目:如要保證線程T1, T2, T3順序執(zhí)行?
考察意圖
在面試中出現(xiàn)這道問題,通常是為了考察候選人的以下幾個(gè)知識(shí)點(diǎn):
- 多線程基礎(chǔ)知識(shí):希望了解候選人是否熟悉Java多線程的基本概念,包括線程的創(chuàng)建、啟動(dòng)和同步機(jī)制。
- 同步機(jī)制的理解:候選人需要展示對(duì)Java中各種同步工具的理解,如join()、CountDownLatch、Semaphore、CyclicBarrier等,并知道如何在不同場(chǎng)景下應(yīng)用這些工具。
- 線程間通信:希望候選人理解線程間通信的基本原理,例如如何使用wait()和notify()來協(xié)調(diào)線程。
- 對(duì)Java并發(fā)包的熟悉程度:希望候選人了解Java并發(fā)包(java.util.concurrent)中的工具和類,展示其對(duì)現(xiàn)代Java并發(fā)編程的掌握。
保證線程順序執(zhí)行的方法
在分析完面試題的考察意圖之后,我們?cè)俜治鋈绾伪WC線程順序執(zhí)行,這里列舉了幾種常見的方式。
1.join()
join()方法是Thread類的一部分,可以讓一個(gè)線程等待另一個(gè)線程完成執(zhí)行。當(dāng)你在一個(gè)線程T上調(diào)用T.join()時(shí),調(diào)用線程將進(jìn)入等待狀態(tài),直到線程T完成(即終止)。因此,可以通過在每個(gè)線程啟動(dòng)后調(diào)用join()來實(shí)現(xiàn)順序執(zhí)行。
如下示例代碼,展示了join()如何保證線程順序執(zhí)行:
Thread t1 = new Thread(() -> {
// 線程T1的任務(wù)
});
Thread t2 = new Thread(() -> {
// 線程T2的任務(wù)
});
Thread t3 = new Thread(() -> {
// 線程T3的任務(wù)
});
t1.start();
t1.join(); // 等待t1完成
t2.start();
t2.join(); // 等待t2完成
t3.start();
t3.join(); // 等待t3完成
2.CountDownLatch
CountDownLatch通過一個(gè)計(jì)數(shù)器來實(shí)現(xiàn),初始時(shí),計(jì)數(shù)器的值由構(gòu)造函數(shù)設(shè)置,每次調(diào)用countDown()方法,計(jì)數(shù)器的值減1。當(dāng)計(jì)數(shù)器的值變?yōu)榱銜r(shí),所有等待在await()方法上的線程都將被喚醒,繼續(xù)執(zhí)行。
CountDownLatch是Java并發(fā)包(java.util.concurrent)中的一個(gè)同步輔助類,用于協(xié)調(diào)多個(gè)線程之間的執(zhí)行順序。它允許一個(gè)或多個(gè)線程等待另外一組線程完成操作。
如下示例代碼,展示了CountDownLatch如何保證線程順序執(zhí)行:
CountDownLatch latch1 = new CountDownLatch(1);
CountDownLatch latch2 = new CountDownLatch(1);
Thread t1 = new Thread(() -> {
// 線程T1的任務(wù)
latch1.countDown(); // 完成后遞減latch1
});
Thread t2 = new Thread(() -> {
try {
latch1.await(); // 等待T1完成
// 線程T2的任務(wù)
latch2.countDown(); // 完成后遞減latch2
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
Thread t3 = new Thread(() -> {
try {
latch2.await(); // 等待T2完成
// 線程T3的任務(wù)
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
t1.start();
t2.start();
t3.start();
CountDownLatch關(guān)鍵方法解析:
- CountDownLatch(int count) : 構(gòu)造函數(shù),創(chuàng)建一個(gè)CountDownLatch實(shí)例,計(jì)數(shù)器的初始值為count。
- void await() : 使當(dāng)前線程等待,直到計(jì)數(shù)器的值變?yōu)榱恪?/li>
- boolean await(long timeout, TimeUnit unit) : 使當(dāng)前線程等待,直到計(jì)數(shù)器的值變?yōu)榱慊虻却龝r(shí)間超過指定的時(shí)間。
- void countDown() : 遞減計(jì)數(shù)器的值。當(dāng)計(jì)數(shù)器的值變?yōu)榱銜r(shí),所有等待的線程被喚醒。
3.Semaphore
Semaphore通過一個(gè)計(jì)數(shù)器來管理許可,計(jì)數(shù)器的初始值由構(gòu)造函數(shù)指定,表示可用許可的數(shù)量。線程可以通過調(diào)用acquire()方法請(qǐng)求許可,如果許可可用則授予訪問權(quán)限,否則線程將阻塞。使用完資源后,線程調(diào)用release()方法釋放許可,從而允許其他阻塞的線程獲取許可。
如下示例代碼,展示了Semaphore如何保證線程順序執(zhí)行:
Semaphore semaphore1 = new Semaphore(0);
Semaphore semaphore2 = new Semaphore(0);
Thread t1 = new Thread(() -> {
// 線程T1的任務(wù)
semaphore1.release(); // 釋放一個(gè)許可
});
Thread t2 = new Thread(() -> {
try {
semaphore1.acquire(); // 獲取許可,等待T1完成
// 線程T2的任務(wù)
semaphore2.release(); // 釋放一個(gè)許可
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
Thread t3 = new Thread(() -> {
try {
semaphore2.acquire(); // 獲取許可,等待T2完成
// 線程T3的任務(wù)
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
t1.start();
t2.start();
t3.start();
Semaphore關(guān)鍵方法分析:
- Semaphore(int permits) :構(gòu)造一個(gè)具有給定許可數(shù)的Semaphore。
- Semaphore(int permits, boolean fair) :構(gòu)造一個(gè)具有給定許可數(shù)的Semaphore,并指定是否是公平的。公平性指的是線程獲取許可的順序是否是先到先得。
- void acquire() :獲取一個(gè)許可,如果沒有可用許可,則阻塞直到有許可可用。
- void acquire(int permits) :獲取指定數(shù)量的許可。
- void release() :釋放一個(gè)許可。
- void release(int permits) :釋放指定數(shù)量的許可。
- int availablePermits() :返回當(dāng)前可用的許可數(shù)量。
- boolean tryAcquire() :嘗試獲取一個(gè)許可,立即返回true或false。
- boolean tryAcquire(long timeout, TimeUnit unit) :在給定的時(shí)間內(nèi)嘗試獲取一個(gè)許可。
4.單線程池
單線程池(Executors.newSingleThreadExecutor())可以確保任務(wù)按提交順序依次執(zhí)行。所有任務(wù)都會(huì)在同一個(gè)線程中運(yùn)行,保證了順序性。
如下示例代碼展示了單線程池如何保證線程順序執(zhí)行:
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(new T1());
executor.submit(new T2());
executor.submit(new T3());
executor.shutdown();
單線程這種方法簡(jiǎn)單易用,適合需要順序執(zhí)行的場(chǎng)景。
5.synchronized
synchronized 是Java中的一個(gè)關(guān)鍵字,用于實(shí)現(xiàn)線程同步,確保多個(gè)線程對(duì)共享資源的訪問是互斥的。它通過鎖機(jī)制來保證同一時(shí)刻只有一個(gè)線程可以執(zhí)行被Synchronized保護(hù)的代碼塊,從而避免數(shù)據(jù)不一致和線程安全問題。
如下示例代碼,展示了synchronized如何保證線程順序執(zhí)行:
class Task {
synchronized void executeTask(String taskName) {
System.out.println(taskName + " 執(zhí)行");
}
}
public class Main {
public static void main(String[] args) {
Task task = new Task();
new Thread(() -> task.executeTask("T1")).start();
new Thread(() -> task.executeTask("T2")).start();
new Thread(() -> task.executeTask("T3")).start();
}
}
總結(jié)
在這篇文章中,我們分析了 5種保證線程順序執(zhí)行的方法,依次如下:
- join()
- CountDownLatch
- Semaphore
- 單線程池
- synchronized
在實(shí)際開發(fā)中,需要在業(yè)務(wù)代碼中去保證線程執(zhí)行順序的情況幾乎不會(huì)出現(xiàn),因此,這個(gè)面試題其實(shí)缺乏實(shí)際的應(yīng)用場(chǎng)景,純粹是為了面試存在。盡管是面試題,還是可以幫助我們更好地去了解和掌握線程。