Java并发编程 - 现有线程T1、T2、T3、T4和T5,如何让线程按T1-T5的顺序依次执行。

1. 使用join()

public class TestJoin {

    static class TestThread extends Thread {
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + "正在运行");
        }
    }

    public static void main(String[] args) {
        Thread t1 = new TestThread();
        Thread t2 = new TestThread();
        Thread t3 = new TestThread();
        Thread t4 = new TestThread();
        Thread t5 = new TestThread();
        try {
            //t1先启动
            t1.start();
            t1.join();
            //t2
            t2.start();
            t2.join();
            //t3
            t3.start();
            t3.join();
            //t4
            t4.start();
            t4.join();
            //t5
            t5.start();
            t5.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Java 7 Concurrency Cookbook 中对join的相关描述:

Waiting for the finalization of a thread

In some situations, we will have to wait for the finalization of a thread. For example, we may have a program that will begin initializing the resources it needs before proceeding with the rest of the execution. We can run the initialization tasks as threads and wait for its finalization before continuing with the rest of the program. For this purpose, we can use the join() method of the Thread class. When we call this method using a thread object, it suspends the execution of the calling thread until the object called finishes its execution.

当我们调用某个线程的这个方法时,这个方法会挂起调用线程,直到被调用线程结束执行,调用线程才会继续执行。

2.使用等待/通知机制

1.wait/notify实现

public class TestNotify {

    private volatile static int value = 1;
    private static final Object lock = new Object();

    private static Thread getThread(int currentValue) {
        return new Thread(() -> {
            try {
                synchronized (lock) {
                    while (value != currentValue) {
                        lock.wait();
                    }
                    System.out.println(Thread.currentThread().getName() + "正在运行");
                    value = currentValue + 1;
                    lock.notifyAll();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }

    public static void main(String[] args) {

        Thread t1 = getThread(1);
        Thread t2 = getThread(2);
        Thread t3 = getThread(3);
        Thread t4 = getThread(4);
        Thread t5 = getThread(5);

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

2.await/signal实现

public class TestCondition {

    volatile private static int value = 1;

    private static Lock lock = new ReentrantLock();
    private static Condition condition1 = lock.newCondition();
    private static Condition condition2 = lock.newCondition();
    private static Condition condition3 = lock.newCondition();
    private static Condition condition4 = lock.newCondition();
    private static Condition condition5 = lock.newCondition();

    private static Thread setCondition(Condition condition, Condition nextCondition, int currentValue) {
        return new Thread(() -> {
            try {
                lock.lock();
                while (value != currentValue) {
                    condition.await();
                }
                System.out.println(Thread.currentThread().getName() + "正在运行");
                value = currentValue + 1;
                nextCondition.signalAll();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        });
    }

    public static void main(String[] args) {

        Thread t1 = setCondition(condition1, condition2, 1);
        Thread t2 = setCondition(condition2, condition3, 2);
        Thread t3 = setCondition(condition3, condition4, 3);
        Thread t4 = setCondition(condition4, condition5, 4);
        Thread t5 = setCondition(condition5, condition1, 5);

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

 

猜你喜欢

转载自www.cnblogs.com/helios-fz/p/11216925.html