public class TestJoin

public static void main(String [] args)

ThreadA a=new ThreadA(); 
ThreadB b=new ThreadB(a); 
//b.setPriority(10);
b.start(); 
a.start(); 

} } 
class ThreadB extends Thread

ThreadA a; 
public ThreadB(ThreadA a)

this.a = a; 
}  public void run()

System.out.println("Thread B has started!"); 
try

a.join(); 
}
catch(InterruptedException e) 
{e.printStackTrace();} 
System.out.println("Thread B is ended!"); 


class ThreadA extends Thread

public void run()

System.out.println("Thread A has started!"); 
try{ sleep(300); 
}catch(InterruptedException e){ 
e.printStackTrace(); 

System.out.println("Thread A ends!"); 

}//将main()方法中注释的代码去掉后再运行一次,出来的结果完全不同,为什么?
//我这个是在windows下运行的,跪求答案

解决方案 »

  1.   

    //b.setPriority(10); 
    这一块影响应该不大吧,
    不懂,等高手。
      

  2.   

    原本我也是这么想的,但是结果却不一样呀~~设置优先级高不代表它一定先抢到啊,join()方法不是会将2个线程合并成一个吗?这里明显是join()进来的线程也是需要分配时间片的嘛...关于这个join()方法的资料我找了好就了,都没有一个地方说的清楚的
      

  3.   

    楼主参照下你发的另外一个帖子:
    http://topic.csdn.net/u/20090211/00/6d78c7e5-560f-4302-a21b-7574178a490a.html?seed=39037960另外,join()方法是会将2个线程合并成一个吗?你是说英文字面解释吗?哪里有这样写?API:public final void join()
                    throws InterruptedException
    等待该线程终止。 抛出: 
    InterruptedException - 如果任何线程中断了当前线程。当抛出该异常时,当前线程的中断状态 被清除。