如何在java中實現跨線程的通信

通常而言,若是沒有干預的話,線程在啓動以後會一直運行到結束,但有時候咱們又須要不少線程來共同完成一個任務,這就牽扯到線程間的通信。多線程

如何讓兩個線程前後執行?Thread.join方法dom

private static void demo2() {
    Thread A = new Thread(new Runnable() {
        @Override
        public void run() {
            printNumber("A");
        }
    });
    Thread B = new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println("B starts waiting for A");
            try {
                A.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            printNumber("B");
        }
    });
    B.start();
    A.start();
}

其中A.join()的意思便是等待A線程執行完畢。ide

如何讓兩個線程交互執行?object.wait和object.notify方法spa

 1 /**
 2  * A 1, B 1, B 2, B 3, A 2, A 3
 3  */
 4 private static void demo3() {
 5     Object lock = new Object();
 6     Thread A = new Thread(new Runnable() {
 7         @Override
 8         public void run() {
 9             synchronized (lock) {
10                 System.out.println("A 1");
11                 try {
12                     lock.wait();
13                 } catch (InterruptedException e) {
14                     e.printStackTrace();
15                 }
16                 System.out.println("A 2");
17                 System.out.println("A 3");
18             }
19         }
20     });
21     Thread B = new Thread(new Runnable() {
22         @Override
23         public void run() {
24             synchronized (lock) {
25                 System.out.println("B 1");
26                 System.out.println("B 2");
27                 System.out.println("B 3");
28                 lock.notify();
29             }
30         }
31     });
32     A.start();
33     B.start();
34 }

A在輸出完1以後等待B的notify纔會去執行其餘的操做。線程

如何讓四個線程的其中一個等待其餘三個執行完畢?CountdownLatch就是幹這個的。code

CountdownLatch的基本用法對象

  1. 建立一個CountdownLatch並賦初始值,CountdownLatch countDownLatch = new CountDownLatch(3;
  2. 在須要等待的線程中調用 countDownLatch.await() 進入等待狀態;
  3. 在其餘線程執行中適當的時候調用 countDownLatch.countDown() ,會使內部否計數值減一;
  4. 當 countDown()致使count值爲 0, 則處於等待態的線程開始執行。
 1 private static void runDAfterABC() {
 2     int worker = 3;
 3     CountDownLatch countDownLatch = new CountDownLatch(worker);
 4     new Thread(new Runnable() {
 5         @Override
 6         public void run() {
 7             System.out.println("D is waiting for other three threads");
 8             try {
 9                 countDownLatch.await();
10                 System.out.println("All done, D starts working");
11             } catch (InterruptedException e) {
12                 e.printStackTrace();
13             }
14         }
15     }).start();
16     for (char threadName='A'; threadName <= 'C'; threadName++) {
17         final String tN = String.valueOf(threadName);
18         new Thread(new Runnable() {
19             @Override
20             public void run() {
21                 System.out.println(tN + "is working");
22                 try {
23                     Thread.sleep(100);
24                 } catch (Exception e) {
25                     e.printStackTrace();
26                 }
27                 System.out.println(tN + "finished");
28                 countDownLatch.countDown();
29             }
30         }).start();
31     }
32 }

如何讓三個線程的各自開始作一些事情,而後在某個時間點上進行同步?CyclicBarrier 是幹這個的。blog

CyclicBarrier 的用法:three

  1. 首先仍是須要先建立一個CyclicBarrier對象,設置初始值,CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
  2. 各個線程同步啓動,在完成一些邏輯以後,調用 cyclicBarrier.await()開始等待;
  3. 當全部的線程都調用了 cyclicBarrier.await() 以後,每一個線程均可以執行以後的邏輯。
 1 private static void runABCWhenAllReady() {
 2     int runner = 3;
 3     CyclicBarrier cyclicBarrier = new CyclicBarrier(runner);
 4     final Random random = new Random();
 5     for (char runnerName='A'; runnerName <= 'C'; runnerName++) {
 6         final String rN = String.valueOf(runnerName);
 7         new Thread(new Runnable() {
 8             @Override
 9             public void run() {
10                 long prepareTime = random.nextInt(10000) + 100;
11                 System.out.println(rN + "is preparing for time:" + prepareTime);
12                 try {
13                     Thread.sleep(prepareTime);
14                 } catch (Exception e) {
15                     e.printStackTrace();
16                 }
17                 try {
18                     System.out.println(rN + "is prepared, waiting for others");
19                     cyclicBarrier.await(); // The current runner is ready, waiting for others to be ready
20                 } catch (InterruptedException e) {
21                     e.printStackTrace();
22                 } catch (BrokenBarrierException e) {
23                     e.printStackTrace();
24                 }
25                 System.out.println(rN + "starts running"); // All the runners are ready to start running together
26             }
27         }).start();
28     }
29 }

如何取回某個線程的返回值了?Callable 是幹這個的。get

先看下定義:

@FunctionalInterface
public interface Callable<V> {
    /**
     * Computes a result, or throws an exception if unable to do so.
     *
     * @return computed result
     * @throws Exception if unable to compute a result
     */
    V call() throws Exception;
}

而後直接給個例子:

 1 private static void doTaskWithResultInWorker() {
 2     Callable<Integer> callable = new Callable<Integer>() {
 3         @Override
 4         public Integer call() throws Exception {
 5             System.out.println("Task starts");
 6             Thread.sleep(1000);
 7             int result = 0;
 8             for (int i=0; i<=100; i++) {
 9                 result += i;
10             }
11             System.out.println("Task finished and return result");
12             return result;
13         }
14     };
15     FutureTask<Integer> futureTask = new FutureTask<>(callable);
16     new Thread(futureTask).start();
17     try {
18         System.out.println("Before futureTask.get()");
19         System.out.println("Result:" + futureTask.get());
20         System.out.println("After futureTask.get()");
21     } catch (InterruptedException e) {
22         e.printStackTrace();
23     } catch (ExecutionException e) {
24         e.printStackTrace();
25     }
26 }

注意,其中futureTask.get()方法是阻塞調用。

 

以上都是一些很基本的應用,在新版本的CompleteFuture中其實提供了更多的鏈式操做,不過寫起來比較複雜,看起來也不清晰。

相關文章
相關標籤/搜索