【Java】同时启动三个线程顺序打印1,2,3

在 Java 中,如果你要启动三个线程,它们分别打印 1、2、3,并且顺序执行(1 → 2 → 3),就不能直接用 Thread.start(),因为线程是异步的,不能保证顺序。要想按顺序控制输出,必须使用线程间同步机制。

1. 使用 CountDownLatch 来控制线程执行顺序。

import java.util.concurrent.CountDownLatch;

public class MultiThreadPrint {
    public static void main(String[] args) {
        CountDownLatch latch2 = new CountDownLatch(1);
        CountDownLatch latch3 = new CountDownLatch(1);
        Thread t1 = new Thread(() -> {
            System.out.println(1);
            latch2.countDown();
        }, "1");
        Thread t2 = new Thread(() -> {
            try {
                latch2.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(2);
            latch3.countDown();
        }, "2");
        Thread t3 = new Thread(() -> {
            try {
                latch3.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(3);
        }, "3");

        t3.start();
        t2.start();
        t1.start();

    }
}

2. 信号量机制Semaphore

import java.util.concurrent.Semaphore;

public class SemaphoreExample {
    public static void main(String[] args) {
        Semaphore s1 = new Semaphore(1); // 先释放 1 → t1 可先执行
        Semaphore s2 = new Semaphore(0); // t2 等待 t1 释放
        Semaphore s3 = new Semaphore(0); // t3 等待 t2 释放

        Thread t1 = new Thread(() -> {
            try {
                s1.acquire();
                System.out.println("1");
                s2.release();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread t2 = new Thread(() -> {
            try {
                s2.acquire();
                System.out.println("2");
                s3.release();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread t3 = new Thread(() -> {
            try {
                s3.acquire();
                System.out.println("3");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        t3.start();
        t2.start();
        t1.start();
    }
}

对比

特性CountDownLatchSemaphore
控制方式等待多个线程完成控制并发线程数量 / 顺序
初始计数固定值(如 3)可设置许可数(如 3)
计数行为只能 countDown() 递减acquire() 减、release()
能否重置不可重置(用完即废)可重复使用
用途一次性等待多个线程完成后再执行某个操作控制资源访问、限流、按序执行
阻塞方法await()(等待倒计数归零)acquire()(获取许可)
唤醒方式countDown() 到 0 自动唤醒所有等待线程release() 主动释放许可,唤醒一个/多个线程
实现原理基于 AQS 的 共享模式(等待计数归 0)基于 AQS 的 共享模式(管理许可数量)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值