✅有三个线程T1,T2,T3如何保证顺序执行?

典型回答

想要让三个线程依次执行,并且严格按照T1,T2,T3的顺序的话,主要就是想办法让三个线程之间可以通信、或者可以排队。

想让多个线程之间可以通信,可以通过join方法实现,还可以通过CountDownLatch、CyclicBarrier和Semaphore来实现通信。

想要让线程之间排队的话,可以通过线程池或者CompletableFuture的方式来实现。

扩展知识

依次执行start方法

在代码中,分别依次调用三个线程的start方法,这种方法是最容易想到的,但是也是最不靠谱的。

代码实现如下,通过执行的话可以发现,数据结果是不固定的:

    public static void main(String[] args) {
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Thread 1 running");
            }
        });


        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Thread 2 running");
            }
        });

        Thread thread3 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Thread 3 running");
            }
        });

        thread1.start();
        thread2.start();
        thread3.start();
    }

以上代码的数据结果每次执行都不固定,所以,没办法满足我们的要求。

使用join

Thread类中提供了一个join方法,他的有以下代码:

public static void main(String[] args) throws InterruptedException {
    Thread thread1 = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Thread 1 running");
        }
    });

    thread1.start();

    System.out.println("Main 1 running");
}

输出结果会是:

Main 1 running
Thread 1 running

但是,如果我们在上面的第15行,增加一行thread1.join();那么输出结果就会有变化,如下:

Thread 1 running
Main 1 running

所以,join就是把thread1这个子线程加入到当前主线程中,也就是主线程要阻塞在这里,等子线程执行完之后再继续执行。

所以,我们可以通过join来实现多个线程的顺序执行:

    public static void main(String[] args) {
        final Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " is Running.");
            }
        },"T1");


        final Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    thread1.join();
                } catch (InterruptedException e) {
                    System.out.println("join thread1 failed");
                }
                System.out.println(Thread.currentThread().getName() + " is Running.");
            }
        },"T2");

        Thread thread3 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    thread2.join();
                } catch (InterruptedException e) {
                    System.out.println("join thread1 failed");
                }
                System.out.println(Thread.currentThread().getName() + " is Running.");
            }
        },"T3");

        thread3.start();
        thread2.start();
        thread1.start();
    }

我们在thread2中等待thread1执行完,然后在thread3中等待thread2执行完。那么整体的执行顺序就是:

T1 is Running.
T2 is Running.
T3 is Running.

使用CountDownLatch

CountDownLatch是Java并发库中的一个同步辅助类,它允许一个或多个线程等待其他线程完成操作。我们可以借助他来让三个线程之间相互通信,以达到顺序执行的目的。

public class CountDownLatchThreadExecute {
    public static void main(String[] args) throws InterruptedException {
                // 创建CountDownLatch对象,用来做线程通信
        CountDownLatch latch = new CountDownLatch(1);
        CountDownLatch latch2 = new CountDownLatch(1);
        CountDownLatch latch3 = new CountDownLatch(1);

        // 创建并启动线程T1
        Thread t1 = new Thread(new MyThread(latch), "T1");
        t1.start();

        // 等待线程T1执行完
        latch.await();

        // 创建并启动线程T2
        Thread t2 = new Thread(new MyThread(latch2), "T2");
        t2.start();

        // 等待线程T2执行完
        latch2.await();

        // 创建并启动线程T3
        Thread t3 = new Thread(new MyThread(latch3), "T3");
        t3.start();

        // 等待线程T3执行完
        latch3.await();
    }
}

class MyThread implements Runnable {
    private CountDownLatch latch;

    public MyThread(CountDownLatch latch) {
        this.latch = latch;
    }

    @Override
    public void run() {
        try {
            // 模拟执行任务
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() + " is Running.");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            // 完成一个线程,计数器减1
            latch.countDown();
        }
    }
}

主要就是想办法让编排三个子线程的主线程阻塞,保证T1执行完再启动T2,T2执行完再启动T3。而这个编排的方式就是想办法知道什么时候子线程执行完,就可以通过CountDownLatch实现。

基于相同的原理, 我们还可以借助CyclicBarrier和Semaphore实现此功能:


public class CyclicBarrierThreadExecute {

    public static void main(String[] args) throws InterruptedException, BrokenBarrierException {
        // 创建CyclicBarrier对象,用来做线程通信
        CyclicBarrier barrier = new CyclicBarrier(2);

        // 创建并启动线程T1
        Thread t1 = new Thread(new MyThread(barrier), "T1");
        t1.start();

        // 等待线程T1执行完
        barrier.await();

        // 创建并启动线程T2
        Thread t2 = new Thread(new MyThread(barrier), "T2");
        t2.start();

        // 等待线程T2执行完
        barrier.await();

        // 创建并启动线程T3
        Thread t3 = new Thread(new MyThread(barrier), "T3");
        t3.start();

        // 等待线程T3执行完
        barrier.await();
    }
}

class MyThread implements Runnable {
    private CyclicBarrier barrier;

    public MyThread(CyclicBarrier barrier) {
        this.barrier = barrier;
    }

    @Override
    public void run() {
        try {
            // 模拟执行任务
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() + " is Running.");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            // 等待其他线程完成
            try {
                barrier.await();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}


借助Semaphore实现此功能:


public class SemaphoreThreadExecute {
    public static void main(String[] args) throws InterruptedException {
        // 创建Semaphore对象,用来做线程通信
        Semaphore semaphore = new Semaphore(1);

        // 等待线程T1执行完
        semaphore.acquire();

        // 创建并启动线程T1
        Thread t1 = new Thread(new MyThread(semaphore), "T1");
        t1.start();

        // 等待线程T2执行完
        semaphore.acquire();

        // 创建并启动线程T2
        Thread t2 = new Thread(new MyThread(semaphore), "T2");
        t2.start();

        // 等待线程T3执行完
        semaphore.acquire();

        // 创建并启动线程T3
        Thread t3 = new Thread(new MyThread(semaphore), "T3");
        t3.start();
    }
}

class MyThread implements Runnable {
    private Semaphore semaphore;

    public MyThread(Semaphore semaphore) {
        this.semaphore = semaphore;
    }

    @Override
    public void run() {
        try {
            // 模拟执行任务
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() + " is Running.");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            // 释放许可证,表示完成一个线程
            semaphore.release();
        }
    }
}

使用线程池

了解线程池的开发者都知道,线程池内部是使用了队列来存储任务的,所以线程的执行顺序会按照任务的提交顺序执行的,但是如果是多个线程同时执行的话,是保证不了先后顺序的,因为可能先提交的后执行了。但是我们可以定义一个只有一个线程的线程池,然后依次的将T1,T2,T3提交给他执行:

public class ThreadPoolThreadExecute {
    public static void main(String[] args) {
        // 创建线程池
        ExecutorService executor = Executors.newSingleThreadExecutor();

        // 创建并启动线程T1
        executor.submit(new MyThread("T1"));

        // 创建并启动线程T2
        executor.submit(new MyThread("T2"));

        // 创建并启动线程T3
        executor.submit(new MyThread("T3"));

        // 关闭线程池
        executor.shutdown();
    }
}

class MyThread implements Runnable {
    private String name;

    public MyThread(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        try {
            // 模拟执行任务
            Thread.sleep(1000);
            System.out.println(name + " is Running.");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

使用CompletableFuture

Java 8引入了CompletableFuture,它是一个用于异步编程的新的强大工具。CompletableFuture提供了一系列的方法,可以用来创建、组合、转换和管理异步任务,并且可以让你实现异步流水线,在多个任务之间轻松传递结果。

如以下实现方式:

public class CompletableFutureThreadExecute {
    public static void main(String[] args) {
        // 创建CompletableFuture对象
        CompletableFuture<Void> future1 = CompletableFuture.runAsync(new MyThread("T1"));

        // 等待线程T1完成
        future1.join();

        // 创建CompletableFuture对象
        CompletableFuture<Void> future2 = CompletableFuture.runAsync(new MyThread("T2"));

        // 等待线程T2完成
        future2.join();

        // 创建CompletableFuture对象
        CompletableFuture<Void> future3 = CompletableFuture.runAsync(new MyThread("T3"));

        // 等待线程T3完成
        future3.join();
    }
}

class MyThread implements Runnable {
    private String name;

    public MyThread(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        try {
            // 模拟执行任务
            Thread.sleep(1000);
            System.out.println(name + " is Running.");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

上面的代码还可以做一些优化:

public class CompletableFutureThreadExecute {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 创建CompletableFuture对象
        CompletableFuture<Void> future = CompletableFuture.runAsync(new MyThread("T1")).thenRun(new MyThread("T2")).thenRun(new MyThread("T3"));
        future.get();
    }
}

class MyThread implements Runnable {
    private String name;

    public MyThread(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        try {
            // 模拟执行任务
            Thread.sleep(1000);
            System.out.println(name + " is Running.");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

原文: https://www.yuque.com/hollis666/xkm7k3/wwqs6n658n4ip0ed