【Java】有 A、B、C 三个线程,如何保证三个线程同时执行?在并发情况下,如何保证三个线程依次执行?如何保证三个线程有序交错执行?

文章探讨了如何使用Java中的CountDownLatch、Volatile和Semaphore等机制来保证在多线程环境下,A、B、C三个线程的同步执行顺序。CountDownLatch用于让线程依次等待,Volatile用于线程间的数据可见性,而Semaphore用于实现线程的有序交错执行。
摘要由CSDN通过智能技术生成

Q1:有 A、B、C 三个线程,如何保证三个线程同时执行?

public class ThreadSafeDemo {
    public static void main(String[] args) throws InterruptedException {
        int size = 3;
        ThreadSafeDemo threadSafeDemo = new ThreadSafeDemo();
        CountDownLatch latch = new CountDownLatch(1);
        for (int i = 0; i < size; i++) {
            new Thread(() -> {
                try {
                    latch.await();
                    System.out.println(System.currentTimeMillis());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
        Thread.sleep(1000);
        latch.countDown();
    }
}

CountDownLatch相当于体育考试场景:4位考生站在起跑线上等待考官下达口令即可往前冲

 

Q2:有 A、B、C 三个线程,在并发情况下,如何保证三个线程依次执行?

public class UseVolatile {
    static volatile int count = 1;

    public static void main(String[] args) {

        Thread t1 = new Thread(() -> {
            while (true) {
                if (count == 1) {
                    try {
                        Thread.sleep(100);
                        for (int i = 0; i < 3; i++) {
                            System.out.println("A" + i);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    count = 2;
                    return;
                }
            }
        });

        Thread t2 = new Thread(() -> {
            while (true) {
                if (count == 2) {
                    try {
                        Thread.sleep(100);
                        for (int i = 0; i < 3; i++) {
                            System.out.println("B" + i);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    count = 3;
                    return;
                }
            }
        });

        Thread t3 = new Thread(() -> {
            while (true) {
                if (count == 3) {
                    try {
                        Thread.sleep(100);
                        for (int i = 0; i < 3; i++) {
                            System.out.println("C" + i);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    count = 1;
                    return;
                }
            }
        });
        t1.start();
        t2.start();
        t3.start();
    }
}

Q3:有 A、B、C 三个线程,如何保证三个线程有序交错执行?

public class OrderThread {
    private static Semaphore s1=new Semaphore(1);
    private static Semaphore s2=new Semaphore(1);
    private static Semaphore s3=new Semaphore(1);
    public static void main(String[] args) {
        try {
            s1.acquire();
            s2.acquire();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(()->{
            while (true){
                try {
                    s1.acquire();
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("A");
                s2.release();
            }
        }).start();

        new Thread(()->{
            while (true){
                try {
                    s2.acquire();
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("B");
                s3.release();
            }
        }).start();

        new Thread(()->{
            while (true){
                try {
                    s3.acquire();
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("C");
                s1.release();
            }
        }).start();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值