日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

如何在java中实现跨线程的通讯

發布時間:2025/4/14 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何在java中实现跨线程的通讯 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一般而言,如果沒有干預的話,線程在啟動之后會一直運行到結束,但有時候我們又需要很多線程來共同完成一個任務,這就牽扯到線程間的通訊。

如何讓兩個線程先后執行?Thread.join方法

private static void demo2() {Thread A = new Thread(new Runnable() {@Overridepublic void run() {printNumber("A");}});Thread B = new Thread(new Runnable() {@Overridepublic 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線程執行完畢。

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

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就是干這個的。

CountdownLatch的基本用法

  • 創建一個CountdownLatch并賦初始值,CountdownLatch countDownLatch = new CountDownLatch(3;
  • 在需要等待的線程中調用?countDownLatch.await()?進入等待狀態;
  • 在其他線程執行中適當的時候調用?countDownLatch.countDown()?,會使內部否計數值減一;
  • 當?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?是干這個的。

    CyclicBarrier?的用法:

  • 首先還是需要先創建一個CyclicBarrier對象,設置初始值,CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
  • 各個線程同步啟動,在完成一些邏輯之后,調用?cyclicBarrier.await()開始等待;
  • 當所有的線程都調用了?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?是干這個的。

    先看下定義:

    @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中其實提供了更多的鏈式操作,不過寫起來比較復雜,看起來也不清晰。

    轉載于:https://www.cnblogs.com/029zz010buct/p/10440520.html

    總結

    以上是生活随笔為你收集整理的如何在java中实现跨线程的通讯的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。