Java併發編程 - 現有線程T一、T二、T三、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的相關描述:ide

Waiting for the finalization of a threadui

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.this

當咱們調用某個線程的這個方法時,這個方法會掛起調用線程,直到被調用線程結束執行,調用線程纔會繼續執行。spa

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

相關文章
相關標籤/搜索