多线程新手,我遇到了一些问题和困惑。:)
public class NewThread implements Runnable {
Thread t;
NewThread() {
t = new Thread(this, "Demo Thread");
System.out.println("Child Thread " + t);
t.start();
}
@Override
public void run() {
try {
for (int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println("Child Interrupted.");
}
System.out.println("Exiting Child Thread.");
}
}
class ThreadDemo {
public static void main(String[] args) {
NewThread t = new NewThread();
try {
for (int i = 5; i > 0; i--) {
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
// TODO: handle exception
System.out.println("Main Thread Interrupted.");
}
System.out.println("Main Thread Exiting.");
}
}
例外输出
我的输出
为什么我的控制台输出与预期输出不同?谢谢你。