它只执行到:
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!"); }
}
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!"); }
}
解决方案 »
- Set不是不保证顺序吗?为什么TreeSet又能以升序输出了?
- BufferedReader流什么情况下到流末尾(readLine()何时返回null)??
- java如何自动的执行一个c语言的程序,然后将运行的结果记录下来呀?
- Jtable 高亮问题
- 急!急!急!java串口通信问题(鸡毛!鸡毛!鸡毛!)
- 父类和子类都有一个同样参数表的构造函数,为什么父类还需要一个无参构造函数?
- 小弟,怎么让每次算出的Float类型的值只取到小数点后2位
- 同志们,ResultSet结果集怎么才能显示在JTABLE中,有原代码么?请搞出来!
- JAVA和C有这么大的差别吗? JAVA初级问题
- 怎样把参数传到一个类里去呢,
- 怎么读取笔记本的红外线接受的数据?
- java的线程的setDaemon()
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!"); }
}
他是等待该线程中止。。在你的run方法还没完成之前 你把Join弄在中间。。总觉得有问题。。我也是个菜鸟。。只能说出这么点东西
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余下部分。
它只执行到:
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。
我在原来的程序上修改了一下,在主线程上进行了修改。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!是不是我的理论正确的呢。