利用CountDownLatch实现的一个简单的线程同步场景
生活随笔
收集整理的這篇文章主要介紹了
利用CountDownLatch实现的一个简单的线程同步场景
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package countdownlatchTest;import java.util.concurrent.CountDownLatch;class Worker { private String name; // 名字 private long workDuration; // 工作持續時間 public Worker(String name, long workDuration) { this.name = name; this.workDuration = workDuration; System.out.println("Worker: " + name + " is assigned with work time: " + workDuration);} public void doWork() { System.out.println(name + " begins to work..."); try { Thread.sleep(workDuration); // 用休眠模擬工作執行的時間 } catch(InterruptedException ex) {ex.printStackTrace(); }System.out.println(name + " has finished the job..."); }
}class WorkerTestThread implements Runnable { private Worker worker; private CountDownLatch cdLatch; public WorkerTestThread(Worker worker, CountDownLatch cdLatch) { this.worker = worker; this.cdLatch = cdLatch; } @Override public void run() { worker.doWork(); // 讓工人開始工作 cdLatch.countDown(); // 工作完成后倒計時次數減1 } }public class CountDownLatchTest {private static final int MAX_WORK_DURATION = 5000; // 最大工作時間 private static final int MIN_WORK_DURATION = 1000; // 最小工作時間 // 產生隨機的工作時間 private static long getRandomWorkDuration(long min, long max) { return (long) (Math.random() * (max - min) + min); } public static void main(String[] args) { /* 2是次數,不是時間數* 將這個CountDownLatch引用賦給工作線程,每次工作線程完成任務后,調用* CountDownLatch.countDown, 將計數器減一。如果技術器減到0,阻塞的await方法* 才會返回,重新獲得控制權*/CountDownLatch latch = new CountDownLatch(2); // should be exactly 2Worker w1 = new Worker("Jerry Worker 1", getRandomWorkDuration(MIN_WORK_DURATION, MAX_WORK_DURATION)); Worker w2 = new Worker("Jerry Worker 2", getRandomWorkDuration(MIN_WORK_DURATION, MAX_WORK_DURATION)); new Thread(new WorkerTestThread(w1, latch)).start(); new Thread(new WorkerTestThread(w2, latch)).start(); // latch.countDown();try { // 僅當CountDownLatch的count降低到0時,這個阻塞的方法才會返回latch.await(); System.out.println("All jobs have been finished!"); } catch (InterruptedException e) { e.printStackTrace();}}
}
總結
以上是生活随笔為你收集整理的利用CountDownLatch实现的一个简单的线程同步场景的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BLDC开发笔记8.过流保护与电流采样要
- 下一篇: 苏州房贷政策,有以下五点