java併發編程 - 線程的join()示例

Java7 API:  http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join() css

public final void join()
                throws InterruptedException
Waits for this thread to die.
An invocation of this method behaves in exactly the same way as the invocation
join(0)
Throws:
InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
public final void join(long millis)
                throws InterruptedException
Waits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.
This implementation uses a loop of this.wait calls conditioned on this.isAlive. As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances.

Parameters:
millis - the time to wait in milliseconds
Throws:
IllegalArgumentException - if the value of millis is negative
InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
public final void join(long millis,
        int nanos)
                throws InterruptedException
Waits at most millis milliseconds plus nanos nanoseconds for this thread to die.
This implementation uses a loop of this.wait calls conditioned on this.isAlive. As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances.

Parameters:
millis - the time to wait in milliseconds
nanos - 0-999999 additional nanoseconds to wait
Throws:
IllegalArgumentException - if the value of millis is negative, or the value of nanos is not in the range 0-999999
InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.

Java Tourial: http://docs.oracle.com/javase/tutorial/essential/concurrency/join.html html

Joins

The join method allows one thread to wait for the completion of another. If t is a Thread object whose thread is currently executing,
t.join();
causes the current thread to pause execution until t's thread terminates. Overloads of join allow the programmer to specify a waiting period. However, as with sleep, join is dependent on the OS for timing, so you should not assume that join will wait exactly as long as you specify.

Like sleep, join responds to an interrupt by exiting with an InterruptedException.

用處 java

     t.jion()使得當前線程暫停,直到線程t終止,才繼續執行。api

    如此,在多線程時,能夠確保線程的執行順序。多線程

代碼1: oracle

public class CustomThread1 extends Thread
{
    public CustomThread1()
    {
        super("CustomThread1");
    }
    @Override
    public void run()
    {
        String threadName = Thread.currentThread().getName();
        System.out.println(threadName + " start.");
        try
        {
            for (int i = 0; i < 5; i++)
            {
                Thread.sleep(1000);
                System.out.println(threadName + " loop at " + i);                
            }
            System.out.println(threadName + " end.");
        } catch (Exception e)
        {
            System.out.println("Exception from " + threadName + ".run");
        }
    }
}

public class CustomThread2 extends Thread
{
    CustomThread1 t1;

    public CustomThread2(CustomThread1 t1)
    {
        super("CustomThread2");
        this.t1 = t1;
    }

    @Override
    public void run()
    {
        String threadName = Thread.currentThread().getName();
        System.out.println(threadName + " start.");
        try
        {
            System.out.println("t1 join again");
            t1.join();
            System.out.println("t1 joined again");
            System.out.println(threadName + " end.");
        } catch (Exception e)
        {
            System.out.println("Exception from " + threadName + ".run");
        }
    }
}

public class JoinTestDemo
{
    public static void main(String[] args)
    {
        String threadName = Thread.currentThread().getName();
        System.out.println(threadName + " start.");
        CustomThread1 t1 = new CustomThread1();
        CustomThread2 t2 = new CustomThread2(t1);
        try
        {
            t1.start();
            Thread.sleep(3000);
            System.out.println("t1 jion");
            t1.join();
            System.out.println("t1 jioned");
            t2.start();
            System.out.println("t2 jion");
            t2.join();
            System.out.println("t2 jioned");
        } catch (Exception e)
        {
            System.out.println("Exception from main");
        }
        
        System.out.println(threadName + " end !");
    }
}

代碼1運行結果: app

main start.
CustomThread1 start.
CustomThread1 loop at 0
CustomThread1 loop at 1
t1 jion
CustomThread1 loop at 2
CustomThread1 loop at 3
CustomThread1 loop at 4
CustomThread1 end.
t1 jioned
t2 jion
CustomThread2 start.
t1 join again
t1 joined again
CustomThread2 end.
t2 jioned
main end !

代碼2: ide

僅僅把代碼1中JoinTestDemo類中main方法的try快修改成以下:oop

t1.start();
            Thread.sleep(3000);
            //System.out.println("t1 jion");
            //t1.join();
            //System.out.println("t1 jioned");
            t2.start();
            //System.out.println("t2 jion");
            //t2.join(); 
            //System.out.println("t2 jioned");

代碼2運行結果: this

main start.
CustomThread1 start.
CustomThread1 loop at 0
CustomThread1 loop at 1
CustomThread1 loop at 2
main end !
CustomThread2 start.
t1 join again
CustomThread1 loop at 3
CustomThread1 loop at 4
CustomThread1 end.
t1 joined again
CustomThread2 end.


參考資料:http://blog.csdn.net/bzwm/article/details/3881392

相關文章
相關標籤/搜索