|
| 1 | +package com.crossoverjie.actual; |
| 2 | + |
| 3 | +import org.slf4j.Logger; |
| 4 | +import org.slf4j.LoggerFactory; |
| 5 | + |
| 6 | +import java.util.concurrent.*; |
| 7 | + |
| 8 | +/** |
| 9 | + * Function:三种方式线程通信 |
| 10 | + * |
| 11 | + * @author crossoverJie |
| 12 | + * Date: 04/01/2018 22:57 |
| 13 | + * @since JDK 1.8 |
| 14 | + */ |
| 15 | +public class ThreadConnect { |
| 16 | + private final static Logger LOGGER = LoggerFactory.getLogger(ThreadConnect.class); |
| 17 | + public static void main(String[] args) throws Exception { |
| 18 | + //join(); |
| 19 | + //executorService(); |
| 20 | + countDownLatch(); |
| 21 | + |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * 使用countDownLatch 每执行完一个就减一,最后等待全部完成 |
| 26 | + * @throws Exception |
| 27 | + */ |
| 28 | + private static void countDownLatch() throws Exception{ |
| 29 | + int thread = 3 ; |
| 30 | + long start = System.currentTimeMillis(); |
| 31 | + CountDownLatch countDown = new CountDownLatch(thread); |
| 32 | + for (int i= 0 ;i<thread ; i++){ |
| 33 | + new Thread(new Runnable() { |
| 34 | + @Override |
| 35 | + public void run() { |
| 36 | + LOGGER.info("thread run"); |
| 37 | + try { |
| 38 | + Thread.sleep(2000); |
| 39 | + countDown.countDown(); |
| 40 | + |
| 41 | + LOGGER.info("thread end"); |
| 42 | + } catch (InterruptedException e) { |
| 43 | + e.printStackTrace(); |
| 44 | + } |
| 45 | + } |
| 46 | + }).start(); |
| 47 | + } |
| 48 | + |
| 49 | + countDown.await(); |
| 50 | + long stop = System.currentTimeMillis(); |
| 51 | + LOGGER.info("main over total time={}",stop-start); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * 利用线程池的 awaitTermination 方法,每隔一秒钟检查线程池是否执行完毕 |
| 56 | + * @throws Exception |
| 57 | + */ |
| 58 | + private static void executorService() throws Exception{ |
| 59 | + BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(10) ; |
| 60 | + ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(5,5,1, TimeUnit.MILLISECONDS,queue) ; |
| 61 | + poolExecutor.execute(new Runnable() { |
| 62 | + @Override |
| 63 | + public void run() { |
| 64 | + LOGGER.info("running"); |
| 65 | + try { |
| 66 | + Thread.sleep(3000); |
| 67 | + } catch (InterruptedException e) { |
| 68 | + e.printStackTrace(); |
| 69 | + } |
| 70 | + } |
| 71 | + }); |
| 72 | + poolExecutor.execute(new Runnable() { |
| 73 | + @Override |
| 74 | + public void run() { |
| 75 | + LOGGER.info("running2"); |
| 76 | + try { |
| 77 | + Thread.sleep(2000); |
| 78 | + } catch (InterruptedException e) { |
| 79 | + e.printStackTrace(); |
| 80 | + } |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + poolExecutor.shutdown(); |
| 85 | + while (!poolExecutor.awaitTermination(1,TimeUnit.SECONDS)){ |
| 86 | + LOGGER.info("线程还在执行。。。"); |
| 87 | + } |
| 88 | + LOGGER.info("main over"); |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + /** |
| 93 | + * 采用 join 线程间通信 |
| 94 | + * @throws InterruptedException |
| 95 | + */ |
| 96 | + private static void join() throws InterruptedException { |
| 97 | + Thread t1 = new Thread(new Runnable() { |
| 98 | + @Override |
| 99 | + public void run() { |
| 100 | + LOGGER.info("running"); |
| 101 | + try { |
| 102 | + Thread.sleep(3000); |
| 103 | + } catch (InterruptedException e) { |
| 104 | + e.printStackTrace(); |
| 105 | + } |
| 106 | + } |
| 107 | + }) ; |
| 108 | + Thread t2 = new Thread(new Runnable() { |
| 109 | + @Override |
| 110 | + public void run() { |
| 111 | + LOGGER.info("running2"); |
| 112 | + try { |
| 113 | + Thread.sleep(4000); |
| 114 | + } catch (InterruptedException e) { |
| 115 | + e.printStackTrace(); |
| 116 | + } |
| 117 | + } |
| 118 | + }) ; |
| 119 | + |
| 120 | + t1.start(); |
| 121 | + //等待线程1终止 |
| 122 | + t1.join(); |
| 123 | + |
| 124 | + t2.start(); |
| 125 | + //等待线程2终止 |
| 126 | + t2.join(); |
| 127 | + |
| 128 | + LOGGER.info("main over"); |
| 129 | + } |
| 130 | +} |
0 commit comments