一、截图示例

二、代码示例
package com.learning.countdownlatch;
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @Author wangyouhui
* @Description 游戏进度
**/
public class Learning_02 {
public static void main(String[] args) {
System.out.println("游戏进度");
CountDownLatch countDownLatch = new CountDownLatch(10);
ExecutorService executorService = Executors.newFixedThreadPool(10);
Random random = new Random();
String[] array = new String[10];
for (int i = 0; i < 10; i++) {
int a = i;
executorService.submit(()->{
for (int j = 0; j <= 100; j++) {
try {
Thread.sleep(random.nextInt(100));
} catch (InterruptedException e) {
e.printStackTrace();
}
array[a] = j + "%";
System.out.print("\r" + Arrays.toString(array));
}
countDownLatch.countDown();
});
}
// 主线程等待
try{
countDownLatch.await();
System.out.println("\n游戏开始");
}catch (InterruptedException e){
e.printStackTrace();
}
executorService.shutdown();
}
}

- 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