public class Joining{
static Thread createThread(final int i, final Thread t1) {
Thread t2 = new Thread(){
public void run(){
System.out.println(i+1);
try{
t1.join();
}catch (InterruptedException e) {
}
System.out.println(i+2);
}
};
System.out.println(i+3);
t2.start();
System.out.println(i+4);
return t2;
}
public static void main(String[] args) {
createThread(10,createThread(20,Thread.currentThread()));
}
}
这个程序难吗,有多少人能看出答案来,看出来的讲解一下列!
static Thread createThread(final int i, final Thread t1) {
Thread t2 = new Thread(){
public void run(){
System.out.println(i+1);
try{
t1.join();
}catch (InterruptedException e) {
}
System.out.println(i+2);
}
};
System.out.println(i+3);
t2.start();
System.out.println(i+4);
return t2;
}
public static void main(String[] args) {
createThread(10,createThread(20,Thread.currentThread()));
}
}
这个程序难吗,有多少人能看出答案来,看出来的讲解一下列!
Thread t2 = new Thread() { public void run() {
System.out.println(Thread.currentThread().getName() + ":" + (i + 1));
try {
t1.join();
} catch (InterruptedException e) {
}
System.out.println(Thread.currentThread().getName() + ":" + (i + 2));
}
};
System.out.println(Thread.currentThread().getName() + ":" + (i + 3));
t2.start();
System.out.println(Thread.currentThread().getName() + ":" + (i + 4));
return t2;
} public static void main(String[] args) {
createThread(10, createThread(20, Thread.currentThread()));
}
} 这样改一下,再单步调试一下,可以观察每一步中每个线程的状态,应该很容易明白了.