Thread的 join方法
//copyright©liupengcheng
//http://www.cnblogs.com/liupengcheng
/**
* join
public final void join()
throws InterruptedException等待该线程终止。
抛出:
InterruptedException - 如果任何线程中断了当前线程。当抛出该异常时,当前线程的中断状态 被清除。
下面的例子 通俗的讲,就是A调用join方法后,只有等A所在的线程不再运行,才会将进程分配出去
* */
//http://www.cnblogs.com/liupengcheng
public class joinThread {
public static void main(String [] args) throws Exception{
ThreadTest5 t = new ThreadTest5();
Thread A = new Thread(t);
Thread B = new Thread(t);
A.start();
A.join(); //此处A调用 Thread的join方法,主函数将线程分配给A,当A运行完毕后,才会将线程释放出去。给其他的对象。
B.start();
for (int i = 1;i < 20;i++)
{
System.out.println("树上掉苹果"+ i);
}
System.out.println("苹果没了");
}
}
//http://www.cnblogs.com/liupengcheng
class ThreadTest5 implements Runnable
{
public void run()
{
for (int i = 1;i < 10;i++)
{
System.out.println(Thread.currentThread().getName()+"吃苹果"+(i));
}
}
}
//http://www.cnblogs.com/liupengcheng
/**
* 运行结果为
* Thread-0吃苹果1
Thread-0吃苹果2
Thread-0吃苹果3
Thread-0吃苹果4
Thread-0吃苹果5
Thread-0吃苹果6
Thread-0吃苹果7
Thread-0吃苹果8
Thread-0吃苹果9
树上掉苹果1
树上掉苹果2
树上掉苹果3
树上掉苹果4
树上掉苹果5
树上掉苹果6
Thread-1吃苹果1
树上掉苹果7
Thread-1吃苹果2
树上掉苹果8
Thread-1吃苹果3
树上掉苹果9
Thread-1吃苹果4
树上掉苹果10
Thread-1吃苹果5
树上掉苹果11
Thread-1吃苹果6
Thread-1吃苹果7
Thread-1吃苹果8
Thread-1吃苹果9
树上掉苹果12
树上掉苹果13
树上掉苹果14
树上掉苹果15
树上掉苹果16
树上掉苹果17
树上掉苹果18
树上掉苹果19
苹果没了
Thread-0 值得就是A所在的线程,当A所在的线程运行完毕后,之后的线程由main主函数和B进程争夺。
*/
//http://www.cnblogs.com/liupengcheng