java并发编程 - 线程的join()示例

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

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

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.

用处

     t.jion()使得当前线程暂停,直到线程t终止,才继续执行。

    如此,在多线程时,可以确保线程的执行顺序。

代码1:

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运行结果:

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:

仅仅把代码1中JoinTestDemo类中main方法的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");

代码2运行结果:

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


转载于:https://my.oschina.net/liuyuanyuangogo/blog/315927

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值