리그 오브 레전드 로딩 진행의 자바 동시 프로그래밍 시뮬레이션

자바 시뮬레이션 리그 오브 레전드 로딩 진행


여기에 사진 설명 삽입
다음과 같이 코드는

    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();
    }

추천

출처blog.csdn.net/e891377/article/details/109250598