手撕:控制三个线程的顺序

问题描述: 确保线程 T2 在 T1 执行完后执行,T3 在 T2 执行完后执行。

解决思路同三个线程交替打印ABC,以下给出其他思路:

方案一:join()

public class ThreadJoinExample {
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            System.out.println("T1 is running");
        });

        Thread t2 = new Thread(() -> {
            try {
                t1.join();
                System.out.println("T2 is running");
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        });

        Thread t3 = new Thread(() -> {
            try {
                t2.join();
                System.out.println("T3 is running");
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        });

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

方案二:CountDownLatch

import java.util.concurrent.CountDownLatch;

public class CountDownLatchExample {
    public static void main(String[] args) {
        CountDownLatch latch1 = new CountDownLatch(1);
        CountDownLatch latch2 = new CountDownLatch(1);

        Thread t1 = new Thread(() -> {
            System.out.println("T1 is running");
            latch1.countDown(); // T1完成后,latch1计数器减1
        });

        Thread t2 = new Thread(() -> {
            try {
                latch1.await(); // 等待T1完成
                System.out.println("T2 is running");
                latch2.countDown(); // T2完成后,latch2计数器减1
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        });

        Thread t3 = new Thread(() -> {
            try {
                latch2.await(); // 等待T2完成
                System.out.println("T3 is running");
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        });
		
        t1.start();
        t2.start();
        t3.start();
    }
}

方案三:CompletableFuture

import java.time.LocalDateTime;
import java.util.concurrent.CompletableFuture;

public class CompletableFutureExample {
    private static void printWithStamp(String value, long sleepTime) {
        try {
            System.out.println(LocalDateTime.now() + ": " + value);
            Thread.sleep(sleepTime);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    public static void main(String[] args) {
        // 创建并启动线程T1
        CompletableFuture<Void> t1 = CompletableFuture.runAsync(() -> 
                printWithStamp("T1 is running", 300));
        // 创建并启动线程T2,依赖于线程T1的完成
        CompletableFuture<Void> t2 = t1.thenRunAsync(() -> 
                printWithStamp("T2 is running", 200));
        // 创建并启动线程T3,依赖于线程T2的完成
        CompletableFuture<Void> t3 = t2.thenRunAsync(() -> 
                printWithStamp("T3 is running", 100));
        // 等待所有线程完成
        t3.join();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值