随笔 - 18, 文章 - 0, 评论 - 8, 引用 - 0
数据加载中……

2017年7月13日

CyclicBarrier 简单举例

一句话解释:预备~~~开始

 1 import java.util.concurrent.BrokenBarrierException;
 2 import java.util.concurrent.CyclicBarrier;
 3 
 4 import org.slf4j.Logger;
 5 import org.slf4j.LoggerFactory;
 6 
 7 public class CyclicBarrierLearn {
 8     
 9     private Logger log = LoggerFactory.getLogger(CyclicBarrierLearn.class);
10     
11     private class Work extends Thread {
12         
13         private String name;
14         private CyclicBarrier cyclicBarrier;
15         
16         public Work(String name, CyclicBarrier cyclicBarrier) {
17             this.name = name;
18             this.cyclicBarrier = cyclicBarrier;
19         }
20         
21         @Override
22         public void run() {
23             try {
24                 log.debug("thread name: " + name + " waiting work");
25                 cyclicBarrier.await();
26                 log.debug("thread name: " + name + " working");
27             } catch (InterruptedException e) {
28                 e.printStackTrace();
29             } catch (BrokenBarrierException e) {
30                 e.printStackTrace();
31             }
32             
33         }
34     }
35     
36     public void cyclicBarrier() {
37         CyclicBarrier cyclicBarrier = new CyclicBarrier(50, new Runnable() {
38             
39             @Override
40             public void run() {
41                 log.debug("let's begin work");
42             }
43         });
44         
45         for (int i = 0; i < cyclicBarrier.getParties(); i++) {
46             Work work = new Work(String.valueOf(i), cyclicBarrier);
47             work.start();
48         }
49         
50     }
51 
52     public static void main(String[] args) {
53         CyclicBarrierLearn cyclicBarrierLearn = new CyclicBarrierLearn();
54         cyclicBarrierLearn.cyclicBarrier();
55 
56     }
57 
58 }
59 

posted @ 2017-07-13 11:39 丑男 阅读(166) | 评论 (0)编辑 收藏

CountDownLatch 简单举例

一句话解释:主线程阻塞,其他线程完成后,主线程被唤醒后继续执行

 1 import java.util.Random;
 2 import java.util.concurrent.CountDownLatch;
 3 
 4 import org.slf4j.Logger;
 5 import org.slf4j.LoggerFactory;
 6 
 7 public class CountDownLatchLearn {
 8     
 9     private Logger log = LoggerFactory.getLogger(CountDownLatchLearn.class);
10     private CountDownLatch countDownLatch;
11     
12     public CountDownLatchLearn() {
13         countDownLatch = new CountDownLatch(50);
14     }
15     
16     public void countDown() {
17         Long count = countDownLatch.getCount();
18         log.debug("countDownLatch count is:" + count.toString());
19         
20         for (int i = 0; i < count; i++) {
21             Work work = new Work(String.valueOf(i), countDownLatch);
22             work.start();
23         }
24         try {
25             countDownLatch.await();
26         } catch (InterruptedException e) {
27             e.printStackTrace();
28         }
29         log.debug("work finish!!!");
30     }
31     
32     private class Work extends Thread {
33         
34         private String name;
35         private CountDownLatch countDownLatch;
36         
37         public Work(String name, CountDownLatch countDownLatch) {
38             this.name = name;
39             this.countDownLatch = countDownLatch;
40         }
41         
42         @Override
43         public void run() {
44             Random r = new Random();
45             int sleep = r.nextInt(2000);
46             try {
47                 log.debug("thread sleep: "+ sleep);
48                 Thread.sleep(sleep);
49             } catch (InterruptedException e) {
50                 e.printStackTrace();
51             }
52             log.debug("thread: " + name + ": do work");
53             countDownLatch.countDown();
54         }
55     }
56 
57     public static void main(String[] args) {
58         System.out.println("main start!!!");
59         
60         CountDownLatchLearn countDownLatchLearn = new CountDownLatchLearn();
61         countDownLatchLearn.countDown();
62         
63         System.out.println("main end!!!");
64     }
65 
66 }

posted @ 2017-07-13 11:18 丑男 阅读(302) | 评论 (0)编辑 收藏