Question:
Given the following code
Code: Select all
class MyThread extends Thread{
String name;
public MyThread(String name){
this.name = name;
}
public void run(){
System.out.println(this.name);
}
}
public class Test{
public static void main(String []args){
MyThread t1 = new MyThread("MyThread 1");
MyThread t2 = new MyThread("MyThread 2");
try{
t1.start();
t1.join();
t2.start();
t2.join();
t1.start();
}catch(Exception e){
e.printStackTrace();
}
}
}
A) not compile;
B)
MyThread 1
MyThread 2
MyThread 1
C) it will throws Runtime Exception
Originally I select B, because I think the cycle of thread, t1 will died due to t1.join, I can start the t1 again.
But the actual output I got is C), which is following:
Do I misunderstanding about Thread.join()?MyThread 1
MyThread 2
java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:705)
at HelloWorld.main(HelloWorld.java:22)