它只执行到: 
Thread B has started! 
Thread A has started! 
Thread A ends! 
然后就停住了!为什么? 
程序如下: 
public class TestJoin{ 
public static void main(String [] args){ 
ThreadA a=new ThreadA(); 
ThreadB b=new ThreadB(); 
b.start(); 
a.start(); } 

class ThreadB extends Thread{ 
public ThreadB(){} 
public void run(){ 
System.out.println("Thread B has started!"); try{ 
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(50); 
}catch(InterruptedException e){ 
e.printStackTrace(); 

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

解决方案 »

  1.   

    你把程序改成这样就行呢:
    public class TestJoin{ 
    public static void main(String [] args){ 
    ThreadA a=new ThreadA(); 
    ThreadB b=new ThreadB(); 
    b.start(); 
    a.start(); } 

    class ThreadB extends Thread{ 
    public ThreadB(){} 
    public void run(){ 
    System.out.println("Thread B has started!"); 
    System.out.println("Thread B is ended!"); 
    try{ 
    join(); 
    }catch(InterruptedException e) 
    {e.printStackTrace();} 

    } class ThreadA extends Thread{ public void run(){ 
    System.out.println("Thread A has started!"); 
    try{ sleep(50); 
    }catch(InterruptedException e){ 
    e.printStackTrace(); 

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

  2.   

    不过我也是凭感觉改出来的,楼主可以去查查API文档仔细弄清楚Join
    他是等待该线程中止。。在你的run方法还没完成之前 你把Join弄在中间。。总觉得有问题。。我也是个菜鸟。。只能说出这么点东西
      

  3.   

    public class TestJoin{ 
    public static void main(String [] args){ 
    ThreadA a=new ThreadA(); 
    ThreadB b=new ThreadB(a); 
    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(50); 
    }catch(InterruptedException e){ 
    e.printStackTrace(); 

    System.out.println("Thread A ends!"); } 
    }稍稍改了下你的程序,,看看就应该明白了,,
    a.join(); 的作用就是等待这个a执行完毕,才接着执行b余下部分。
      

  4.   

    调用john(),保证当前线程停止执行,直到该线程加入的线程完成为止(也就是说该线程调用wait())。如果它加入的线程没有存活,这当前线程不需停止。
      

  5.   

    4楼已经纠正了,我就说说输出吧
    它只执行到: 
    Thread B has started! 
    Thread A has started! 
    Thread A ends! 
    然后就停住了!为什么? ThreadA a=new ThreadA(); 
    ThreadB b=new ThreadB(); 
    b.start(); 
    a.start(); b.start()输出Thread B has started! 然后线程b join(). 这里线程b会等待其他线程来终止自己,如果没有的话,线程b会一直等待。
    a.start()输出Thread A has started! 然后线程a sleep了一下,当他sleep完了输出Thread A ends.所以结果就是
    Thread B has started! 
    Thread A has started! 
    Thread A ends! 线程b始终是join。
      

  6.   

    是不是应该这样理解。join()函数是等待自己的线程结束。
    我在原来的程序上修改了一下,在主线程上进行了修改。public class TestJoin {
    public static void main(String[] args) {
    ThreadA a = new ThreadA();
    ThreadB b = new ThreadB();
    b.start();
    a.start();
    try {
    Thread.sleep(5000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    b.interrupt();
    }
    }class ThreadB extends Thread { public ThreadB() {
    } public void run() {
    System.out.println("Thread B has started!"); try {
    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(50);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println("Thread A ends!"); }
    }
    可以看到打印的结果:Thread A has started!
    Thread B has started!
    Thread A ends!
    java.lang.InterruptedException
    at java.lang.Object.wait(Native Method)
    at java.lang.Thread.join(Unknown Source)
    at java.lang.Thread.join(Unknown Source)
    at join的问题.ThreadB.run(TestJoin.java:25)
    Thread B is ended!是不是我的理论正确的呢。