public static void main(String[] args) throws InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(10);
CountDownLatch countDownLatch = new CountDownLatch(10);
Random random = new Random();
String[] allPlayers = new String[10];
for(int i=0;i<10;i++){
int tempPlayer = i ;
executorService.execute(()->{
for (int j = 0; j <= 100; j++) {
try {
Thread.sleep(random.nextInt(100));
} catch (InterruptedException e) {
e.printStackTrace();
}
allPlayers[tempPlayer]=j+"%";
System.out.print("\r"+ Arrays.toString(allPlayers));
}
countDownLatch.countDown();
});
}
countDownLatch.await();
System.out.println("\nready");
executorService.shutdown();
}
用线程池模拟加载进度
于 2022-03-09 14:08:39 首次发布