CountDownLatch使用案例

 案例一


/**
 * CountDownLatch使用案例,配合await()方法使用,线程执行完毕会阻塞在await()这,直至所有线程执行完毕
 */
public class CountDownLatchExample {
    public static void main(String[] args) throws InterruptedException {
        int numThreads = 3;
        CountDownLatch latch = new CountDownLatch(numThreads);

        for (int i = 0; i < numThreads; i++) {
            Thread thread = new Thread(new Worker(latch));
            thread.start();
        }

        latch.await(); // 主线程等待所有子线程完成任务

        System.out.println("All threads have completed their tasks. Main thread can continue.");
    }

    static class Worker implements Runnable {
        private CountDownLatch latch;

        public Worker(CountDownLatch latch) {
            this.latch = latch;
        }

        @Override
        public void run() {
            try {
                // 模拟子线程执行任务
                Thread.sleep((long) (Math.random() * 1000));
                System.out.println("Task completed by thread: " + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                latch.countDown(); // 子线程完成任务后调用countDown减少计数器
            }
        }
    }
}

输出结果:

Task completed by thread: Thread-2
Task completed by thread: Thread-0
Task completed by thread: Thread-1
All threads have completed their tasks. Main thread can continue.

 案例二


/**
 * 使用CountDownLatch完成多个游戏玩家进度条的加载
 * 直到所有玩家进度条加载完毕,游戏就可以启动了
 */
public class ProgressBarWithCountDownLatch {
    public static void main(String[] args) throws InterruptedException {
        int numThreads = 3;
        CountDownLatch latch = new CountDownLatch(numThreads);

        for (int i = 0; i < numThreads; i++) {
            Thread thread = new Thread(new Task(latch, "Thread-" + i));
            thread.start();
        }

        latch.await(); // 等待所有线程加载完毕

        System.out.println("All progress bars have completed. Main thread can continue.");
    }

    static class Task implements Runnable {
        private CountDownLatch latch;
        private String threadName;

        public Task(CountDownLatch latch, String threadName) {
            this.latch = latch;
            this.threadName = threadName;
        }

        @Override
        public void run() {
            System.out.println("Thread " + threadName + " started loading progress bar");
            for (int i = 0; i <= 100; i += 10) {
                updateProgressBar(threadName, i);
                try {
                    Thread.sleep((long) (20 * Math.random()));   // 模拟加载进度
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("Thread " + threadName + " finished loading progress bar");
            latch.countDown(); // 减少计数器
        }

        static void updateProgressBar(String threadName, int progress) {
            // 更新进度条的显示
            System.out.println("Thread " + threadName + " - Progress: " + progress + "%");
        }
    }
}

输出结果

Thread Thread-0 started loading progress bar
Thread Thread-2 started loading progress bar
Thread Thread-2 - Progress: 0%
Thread Thread-1 started loading progress bar
Thread Thread-0 - Progress: 0%
Thread Thread-1 - Progress: 0%
Thread Thread-2 - Progress: 10%
Thread Thread-2 - Progress: 20%
Thread Thread-0 - Progress: 10%
Thread Thread-1 - Progress: 10%
Thread Thread-2 - Progress: 30%
Thread Thread-1 - Progress: 20%
Thread Thread-0 - Progress: 20%
Thread Thread-1 - Progress: 30%
Thread Thread-1 - Progress: 40%
Thread Thread-2 - Progress: 40%
Thread Thread-0 - Progress: 30%
Thread Thread-2 - Progress: 50%
Thread Thread-0 - Progress: 40%
Thread Thread-0 - Progress: 50%
Thread Thread-1 - Progress: 50%
Thread Thread-1 - Progress: 60%
Thread Thread-2 - Progress: 60%
Thread Thread-2 - Progress: 70%
Thread Thread-0 - Progress: 60%
Thread Thread-0 - Progress: 70%
Thread Thread-1 - Progress: 70%
Thread Thread-1 - Progress: 80%
Thread Thread-2 - Progress: 80%
Thread Thread-2 - Progress: 90%
Thread Thread-0 - Progress: 80%
Thread Thread-2 - Progress: 100%
Thread Thread-0 - Progress: 90%
Thread Thread-1 - Progress: 90%
Thread Thread-2 finished loading progress bar
Thread Thread-0 - Progress: 100%
Thread Thread-1 - Progress: 100%
Thread Thread-0 finished loading progress bar
Thread Thread-1 finished loading progress bar
All progress bars have completed. Main thread can continue.

Process finished with exit code 0

你可能感兴趣的:(java,开发语言)