@Slf4j(topic = "c.CountDownLatchDemo")
public class CountDownLatchDemo {
public static void main(String[] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(10);
ExecutorService service = Executors.newFixedThreadPool(10);
Random r = new Random();
String[] all = new String[10];
for (int j = 0; j < 10; j++) {
int k = j;
service.submit(()->{
for (int i = 0; i <= 100 ; i++) {
try {
Thread.sleep(r.nextInt(100));
} catch (InterruptedException e) {
e.printStackTrace();
}
all[k] = i + "%";
System.out.print("\r" + Arrays.toString(all));
}
latch.countDown();
});
}
latch.await();
System.out.println("\n游戏开始");
service.shutdown();
}
}
CountDownLatch小例子
最新推荐文章于 2022-11-27 20:50:29 发布