大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > Java技巧 > Java中线程顺序执行

Java中线程顺序执行

关键词:线程顺序执行Java  阅读(741) 赞(19)

[摘要]本文是对Java中线程顺序执行的讲解,对学习Java编程技术有所帮助,与大家分享。

现有线程threadone、threadtwo和threadthree,想要的运转顺序为threadone->threadtwo->threadthree,应该如何处置?这里需求用到一个复杂的线程办法join().

join()办法的阐明:join办法挂起以后调用线程,直到被调用线程完成后在持续执行(join() method suspends the execution of the calling threaduntil the object called finishes its execution).

上面看一个例子:

/**
 * Thread one for test.
 */
public class ThreadOne implements Runnable {
    @Override
    public void run() {
        String threadName = Thread.currentThread().getName();
        System.out.println(threadName + " start...");


        System.out.println(threadName + " end.");
    }
}
/**
 * Thread two for test.
 */
public class ThreadTwo implements Runnable {
    @Override
    public void run() {
        String threadName = Thread.currentThread().getName();
        System.out.println(threadName + " start...");


        System.out.println(threadName + " end.");
    }
}
/**
 * Thread three for test.
 */
public class ThreadThree implements Runnable {
    @Override
    public void run() {
        String threadName = Thread.currentThread().getName();
        System.out.println(threadName + " start...");


        System.out.println(threadName + " end.");
    }
}
public class JoinMainTest {
    public static void main(String[] args) {
        String threadName = Thread.currentThread().getName();
        System.out.println(threadName + " start...");

        Thread firstThread = new Thread(new ThreadOne());
        Thread secondThread = new Thread(new ThreadTwo());
        Thread thirdThread = new Thread(new ThreadThree());

        // 1. no order thread run
        firstThread.start();
        secondThread.start();
        thirdThread.start();

        System.out.println(threadName + " end.");
    }
}

下面失掉的后果如下:

/**
*
main start...
Thread-0 start...
main end.
Thread-0 end.
Thread-1 start...
Thread-1 end.
Thread-2 start...
Thread-2 end.
*/

public class JoinMainTest {
    public static void main(String[] args) {
        String threadName = Thread.currentThread().getName();
        System.out.println(threadName + " start...");

        Thread firstThread = new Thread(new ThreadOne());
        Thread secondThread = new Thread(new ThreadTwo());
        Thread thirdThread = new Thread(new ThreadThree());
    
        // 2. thread run in order
        try {
            firstThread.start();
            firstThread.join();
            secondThread.start();
            secondThread.join();
            thirdThread.start();
            thirdThread.join();
        } catch (Exception ex) {
            System.out.println("thread join exception.");
        }
        System.out.println(threadName + " end.");
    }
}

这里失掉的后果就是顺序执行的了:

/**
*
main start...
Thread-0 start...
Thread-0 end.
Thread-1 start...
Thread-1 end.
Thread-2 start...
Thread-2 end.
main end.
*/

再来看一下join()的源码,join()调用的就是join(0).可以看到,其实join就是wait,直到线程执行完成。

public final synchronized void join(long millis) throws InterruptedException {
    long base = System.currentTimeMillis();
    long now = 0;

    if (millis < 0) {
        throw new IllegalArgumentException("timeout value is negative");
    }

    if (millis == 0) {
        while (isAlive()) {
            wait(0);
        }
    } else {
        while (isAlive()) {
            long delay = millis - now;
            if (delay <= 0) {
                break;
            }
            wait(delay);
            now = System.currentTimeMillis() - base;
        }
    }
}


相关评论