码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-02-26 22:21:30      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:实现   ini   通讯   waiting   nbsp   ++   res   interrupt   线程   

一般而言,如果没有干预的话,线程在启动之后会一直运行到结束,但有时候我们又需要很多线程来共同完成一个任务,这就牵扯到线程间的通讯。

如何让两个线程先后执行?Thread.join方法

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线程执行完毕。

如何让两个线程交互执行?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的基本用法

  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 是干这个的。

CyclicBarrier 的用法:

  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 是干这个的。

先看下定义:

@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中其实提供了更多的链式操作,不过写起来比较复杂,看起来也不清晰。

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

标签:实现   ini   通讯   waiting   nbsp   ++   res   interrupt   线程   

原文地址:https://www.cnblogs.com/029zz010buct/p/10440520.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!