-
countdownlatch
2021-01-22 00:37:33countdownlatch 是一个同步类工具,不涉及锁定,当count的值为零时当前线程继续运行,不涉及同步,只涉及线程通信的时候,使用它较为合适 public class testLatch { public static void main(String[] args) { ...countdownlatch 是一个同步类工具,不涉及锁定,当count的值为零时当前线程继续运行,不涉及同步,只涉及线程通信的时候,使用它较为合适
public class testLatch {
public static void main(String[] args) { CountDownLatch begin = new CountDownLatch(1); CountDownLatch end = new CountDownLatch(2); for(int i=0; i<2; i++){ Thread thread = new Thread(new Player(begin,end),String.valueOf(i)); thread.start(); } try{ System.out.println("the race begin"); begin.countDown(); end.await();//await() 方法具有阻塞作用,也就是说主线程在这里暂停 System.out.println("the race end"); }catch(Exception e){ e.printStackTrace(); } }
}
class Player implements Runnable{
private CountDownLatch begin; private CountDownLatch end; Player(CountDownLatch begin,CountDownLatch end){ this.begin = begin; this.end = end; } public void run() { try { System.out.println(Thread.currentThread().getName() + " start !");; begin.await();//因为此时已经为0了,所以不阻塞 System.out.println(Thread.currentThread().getName() + " arrived !"); end.countDown();//countDown() 并不是直接唤醒线程,当end.getCount()为0时线程会自动唤醒 } catch (Exception e) { e.printStackTrace(); } }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
主线程main被end.await();阻塞,两个副线程继续往下运行,因为benin已经为0,所以不阻塞,操作两次end的downcount之后,主线程继续往下执行。如果不理解可以将线程数改成3试一下注:end.countDown() 可以在多个线程中调用 计算调用次数是所有线程调用次数的总和
-
CountDownLatch
2020-08-21 14:01:52CountDownLatch countDownLatch = new CountDownLatch(2); //创建pool int corePoolSize = 2; int maximumPoolSize = 10; long keepAliveTime = 120; TimeUnit timeUnit = TimeUnit.SECONDS; BlockingDe..public static void main(String[] args) { //声明countDownLatch CountDownLatch countDownLatch = new CountDownLatch(2); //创建pool int corePoolSize = 2; int maximumPoolSize = 10; long keepAliveTime = 120; TimeUnit timeUnit = TimeUnit.SECONDS; BlockingDeque<Runnable> blockingDeque = new LinkedBlockingDeque<>(10); ThreadPoolExecutor pool = new ThreadPoolExecutor(corePoolSize,maximumPoolSize,keepAliveTime,timeUnit,blockingDeque); //创建callable1,callable2 Callable<Long> callable1 = new Callable<Long>() { @Override public Long call() throws Exception { System.out.println("callable1 begin"); Thread.sleep(10000); System.out.println("callable1 end"); countDownLatch.countDown(); System.out.println("还需等待" + countDownLatch.getCount() + "个线程"); return 1L; } }; Callable<Long> callable2 = new Callable<Long>() { @Override public Long call() throws Exception { System.out.println("callable2 begin"); Thread.sleep(3000); System.out.println("callable2 end"); countDownLatch.countDown(); System.out.println("还需等待" + countDownLatch.getCount() + "个线程"); return 1L; } }; //执行 try { System.out.println("thread start"); Future<Long> future1 = pool.submit(callable1); Future<Long> future2 = pool.submit(callable2); countDownLatch.await(); System.out.println(future1.isDone()); System.out.println(future2.isDone()); System.out.println("thread end"); }catch (InterruptedException e){ e.printStackTrace(); } }
-
CountdownLatch
2020-03-01 15:09:53调用一个CountDownLatch对象的await()方法,其他的任务执行完自己的任务后调用同一个CountDownLatch对象上的countDown()方法,这个调用await()方法的任务将一直阻塞等待,直到这个CountDownLatch对象的计数值减到0...CountdownLatch
调用一个CountDownLatch对象的await()方法,其他的任务执行完自己的任务后调用同一个CountDownLatch对象上的countDown()方法,这个调用await()方法的任务将一直阻塞等待,直到这个CountDownLatch对象的计数值减到0为止。
//测试类 public class CountdownLatchTest { public static void main(String[] args) { CountDownLatch countDownLatch = new CountDownLatch(3); Boss boss = new Boss(countDownLatch); new Thread(new Worker(countDownLatch)).start(); new Thread(new Worker(countDownLatch)).start(); new Thread(new Worker(countDownLatch)).start(); boss.waitWorker(); System.out.println("完成工作,下班了"); } } //工作类 public class Worker implements Runnable { private CountDownLatch count; public Worker(CountDownLatch countDownLatch) { this.count = countDownLatch; } @Override public void run() { Random random = new Random(); int i = random.nextInt(10000); try { System.out.println(Thread.currentThread().getName() + "工作"); Thread.sleep(i); } catch (InterruptedException e) { e.printStackTrace(); } count.countDown();//成功执行扣减CountdowmLatch的数量,如果没成功扣减会导致最后不能继续往下执行 System.out.println(Thread.currentThread().getName() + "去吃个下午茶先");//干完事就直接去,不会等 } } //大佬 public class Boss { CountDownLatch countDownLatch; public Boss(CountDownLatch countDownLatch){ this.countDownLatch = countDownLatch; } public void waitWorker(){ try { countDownLatch.await(); //等待需要发生在其他执行线程之后 System.out.println("开始工作了"); Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } }
收藏数
13,763
精华内容
5,505