Backend Development 5 min read

Using CountDownLatch in Java to Synchronize Multiple Threads for a Lucky Card Collection Example

This article explains the Java CountDownLatch utility, demonstrates its countdown synchronization mechanism with a lucky‑card‑collection scenario, provides a complete runnable example, and shows how the main thread waits for all worker threads to finish before proceeding to the lottery step.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Using CountDownLatch in Java to Synchronize Multiple Threads for a Lucky Card Collection Example

In Java multithreaded programming, many utility classes provide powerful features that satisfy everyday development needs.

This article introduces CountDownLatch , which allows one or more threads to wait for other threads to complete. It works by initializing a positive counter; each time a thread finishes its work, the counter is decremented, and when the count reaches zero the waiting threads are released.

To illustrate a typical use case, the article presents a "lucky card collection" scenario: during a holiday lottery, five parallel tasks simulate collecting cards, and the main thread waits until all five tasks finish before allowing participation in the draw.

package com.sample.interview.multithread; import java.util.Random; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; public class LuckyCollection { public static void main(String[] args) throws Exception { // 初始化倒计时为5 final CountDownLatch latch = new CountDownLatch(5); new Thread(new LuckyCard(latch, "任务1")).start(); new Thread(new LuckyCard(latch, "任务2")).start(); new Thread(new LuckyCard(latch, "任务3")).start(); new Thread(new LuckyCard(latch, "任务4")).start(); new Thread(new LuckyCard(latch, "任务5")).start(); // 主线线程等待这5个线程都结束 latch.await(); System.out.println("集卡完成,可以参与抽奖"); } static class LuckyCard implements Runnable { private final CountDownLatch latch; private final String name; public LuckyCard(CountDownLatch latch, String name) { this.latch = latch; this.name = name; } @Override public void run() { try { System.out.println(name + "开始集卡"); Random random = new Random(); int randomNum = random.nextInt(1001) + 1000; // 模拟收集卡的动作 TimeUnit.MICROSECONDS.sleep(randomNum); System.out.println(name + "集卡完成"); } catch (Exception e) { System.out.println("出错了"); } finally { // 任务完成后计数器减1 latch.countDown(); } } } }

Running the program produces output similar to the following (the exact order varies because the threads run concurrently), and it always ends with the final message indicating that all cards have been collected and the lottery can be entered:

任务5开始集卡 任务2开始集卡 任务4开始集卡 任务3开始集卡 任务1开始集卡 任务5集卡完成 任务3集卡完成 任务2集卡完成 任务1集卡完成 任务4集卡完成 集卡完成,可以参与抽奖

The article also invites readers to follow the "Internet Full‑Stack Architecture" WeChat public account for further programming tips and provides links to related articles on MySQL deadlocks and index invalidation.

JavaConcurrencySynchronizationMultithreadingCountDownLatch
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.