Java concurrent programming-simulation of League of Legends loading progress

java simulated League of Legends loading progress

The
Insert picture description here
code is as follows

    private static void test2() throws InterruptedException {
    
    
        AtomicInteger num = new AtomicInteger(0);
        ExecutorService service = Executors.newFixedThreadPool(10, (r) -> {
    
    
            return new Thread(r, "t" + num.getAndIncrement());
        });
        CountDownLatch latch = new CountDownLatch(10);
        String[] all = new String[10];
        Random r = new Random();
        for (int j = 0; j < 10; j++) {
    
    
            int x = j;
            service.submit(() -> {
    
    
                for (int i = 0; i <= 100; i++) {
    
    
                    try {
    
    
                        Thread.sleep(r.nextInt(100));
                    } catch (InterruptedException e) {
    
    
                    }
                    all[x] = Thread.currentThread().getName() + "(" + (i + "%") + ")";
                    System.out.print("\r" + Arrays.toString(all));
                }
                latch.countDown();
            });
        }
        latch.await();
        System.out.println("\n游戏开始...");
        service.shutdown();
    }
    
    public static void main(String[] args) throws InterruptedException, ExecutionException {
    
    
        test2();
    }

Guess you like

Origin blog.csdn.net/e891377/article/details/109250598