How to ensure that the three threads execute sequentially?

Scenario: There are three threads t1, t2, t3. Ensure that the three threads t1 are executed after t2 is executed, and t3 is executed after t2 is executed.

1. Use join
thread.Join to join the specified thread to the current thread. Two alternately executed threads can be merged into sequential execution threads. For example, the Join() method of thread A is called in thread B, and thread B will not continue to execute until thread A has finished executing.

t.join(); //Call the join method and wait for thread t to finish
executing t.join(1000); //Wait for t thread, the waiting time is 1000 milliseconds.

One, join

package Thread;

/**
 * @author Muluo
 * @create 2021-03-13 8:13
 */
public class testThreadt1t2t3 {
    
    
    public static void main(String[] args) {
    
    

        Thread thread1 = new Thread(new work(null));
        Thread thread2 = new Thread(new work(thread1));
        Thread thread3 = new Thread(new work(thread2));
        thread1.setName("线程一");
        thread2.setName("线程二");
        thread3.setName("线程三");
        thread1.start();
        thread2.start();
        thread3.start();


    }

}
class work implements Runnable {
    
    
    private Thread thread;

    public work(Thread thread) {
    
    
        this.thread = thread;
    }

    @Override
    public void run() {
    
    
        if (thread != null) {
    
    
            try {
    
    
                thread.join();
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());

        } else {
    
    
            System.out.println(Thread.currentThread().getName());
        }
    }
}

Two, CountDownLatch

CountDownLatch (Latch) is a very useful tool class, with which we can intercept one or more threads to execute after a certain condition is mature. It provides a counter inside. The initial value of the counter must be specified when constructing the lock, and the initial value of the counter must be greater than 0. In addition, it also provides a countDown method to manipulate the value of the counter. Each time the countDown method is called, the counter will decrease by 1, until the value of the counter decreases to 0, it means the condition is mature. All threads blocked by calling the await method will be awakened. . This is the internal mechanism of CountDownLatch, it seems very simple, it is nothing more than blocking a part of the thread to let it execute after a certain condition is reached.

public class ThreadTest2 {
    
    
 
// T1、T2、T3三个线程顺序执行
public static void main(String[] args) {
    
    
    CountDownLatch c0 = new CountDownLatch(0); //计数器为0
    CountDownLatch c1 = new CountDownLatch(1); //计数器为1
    CountDownLatch c2 = new CountDownLatch(1); //计数器为1
 
    Thread t1 = new Thread(new Work(c0, c1));
    //c0为0,t1可以执行。t1的计数器减1
 
    Thread t2 = new Thread(new Work(c1, c2));
    //t1的计数器为0时,t2才能执行。t2的计数器c2减1
 
    Thread t3 = new Thread(new Work(c2, c2));
    //t2的计数器c2为0时,t3才能执行
 
    t1.start();
    t2.start();
    t3.start();
 
}
 
//定义Work线程类,需要传入开始和结束的CountDownLatch参数
static class Work implements Runnable {
    
    
    CountDownLatch c1;
    CountDownLatch c2;
 
    Work(CountDownLatch c1, CountDownLatch c2) {
    
    
        super();
        this.c1 = c1;
        this.c2 = c2;
    }
 
    public void run() {
    
    
        try {
    
    
            c1.await();//前一线程为0才可以执行
            System.out.println("thread start:" + Thread.currentThread().getName());
            c2.countDown();//本线程计数器减少
        } catch (InterruptedException e) {
    
    
        }
 
    }
 }
}

Guess you like

Origin blog.csdn.net/qq_43518425/article/details/114727198