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

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();
    }
}

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

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();
    }
}

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

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