新的多线程和我遇到了一些问题和困惑。 :)多线程控制台输出不同
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.");
}
}
的例外输出
我的输出
为什么我的控制台输出与预期输出有什么不同?谢谢。
你确定你有'NewThread t = new NewThread();'main? –
是的,看起来像。 – AppSensei
src对我来说很不错。我能够执行你的源代码并获得正确的输出。你确定这里发布的src和你正在测试的一样吗? – Jayamohan