电商网站建设去迅法网,老榕树网站建设教学,人工智能培训公司,凡科网站可以做seo优化CountDownLatch 倒数的门栓
CountDownLatch latch new CountDownLatch(threads.length); 创建一个门栓,在门栓上面记个数每一个线程结束就countDown 开启线程latch.await(); 每一个线程结束后线程数减一#xff0c;当 latch.await()为0的时候门栓就打开 package com.mas…CountDownLatch 倒数的门栓
CountDownLatch latch new CountDownLatch(threads.length); 创建一个门栓,在门栓上面记个数每一个线程结束就countDown 开启线程latch.await(); 每一个线程结束后线程数减一当 latch.await()为0的时候门栓就打开 package com.mashibing.juc.c_020;import java.util.concurrent.CountDownLatch;public class T06_TestCountDownLatch {public static void main(String[] args) {usingJoin();usingCountDownLatch();}private static void usingCountDownLatch() {Thread[] threads new Thread[100];//创建一个门栓在门栓上面记个数CountDownLatch latch new CountDownLatch(threads.length);for(int i0; ithreads.length; i) { //每一个线程结束就countDownthreads[i] new Thread(()-{int result 0;for(int j0; j10000; j) result j;latch.countDown();});}//线程开启for (int i 0; i threads.length; i) {threads[i].start();}try { //门栓拴住不动latch.await(); } catch (InterruptedException e) {e.printStackTrace();}System.out.println(end latch);}private static void usingJoin() {Thread[] threads new Thread[100];for(int i0; ithreads.length; i) {threads[i] new Thread(()-{int result 0;for(int j0; j10000; j) result j;});}for (int i 0; i threads.length; i) {threads[i].start();}for (int i 0; i threads.length; i) {try {threads[i].join();} catch (InterruptedException e) {e.printStackTrace();}}System.out.println(end join);}
}