码迷,mamicode.com
首页 > 编程语言 > 详细

Java中线程顺序执行

时间:2014-05-28 21:14:55      阅读:351      评论:0      收藏:0      [点我收藏+]

标签:style   c   class   blog   code   java   

现有线程threadone、threadtwo和threadthree,想要的运行顺序为threadone->threadtwo->threadthree,应该如何处理?这里需要用到一个简单的线程方法join().

join()方法的说明:join方法挂起当前调用线程,直到被调用线程完成后在继续执行(join() method suspends the execution of the calling thread until the object called finishes its execution).

下面看一个例子:

bubuko.com,布布扣
/**
 * 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.");
    }
}
bubuko.com,布布扣
bubuko.com,布布扣
/**
 * 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.");
    }
}
bubuko.com,布布扣
bubuko.com,布布扣
/**
 * 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.");
    }
}
bubuko.com,布布扣
bubuko.com,布布扣
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.");
    }
}
bubuko.com,布布扣

上面得到的结果如下:

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

bubuko.com,布布扣
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.");
    }
}
bubuko.com,布布扣

这里得到的结果就是顺序执行的了:

/**
*
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,直到线程执行完成。

bubuko.com,布布扣
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;
        }
    }
}
bubuko.com,布布扣

 

Java中线程顺序执行,布布扣,bubuko.com

Java中线程顺序执行

标签:style   c   class   blog   code   java   

原文地址:http://www.cnblogs.com/treerain/p/java_thread_join.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!