我正在編寫一個小程序,以瞭解如何在Java中運行多個線程。不知道爲什麼我沒有得到任何輸出:Java多線程演示不工作
class NuThread implements Runnable {
NuThread() {
Thread t = new Thread(this, "NuThread");
t.start();
}
public void run() {
try {
for (int i=0; i<5; i++) {
Thread th = Thread.currentThread();
System.out.println(th.getName() + ": " + i);
Thread.sleep(300);
}
} catch (InterruptedException e) {
Thread th = Thread.currentThread();
System.out.println(th.getName() + " interrupted.");
}
}
}
public class MultiThreadDemo {
public static void main(String[] args) {
NuThread t1, t2, t3;
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("Main interrupted.");
}
}
}
謝謝!這就是Herbert Schildt的書中如何創建線索,雖然我確實認爲你有一點。其他東西(main?)應該是創建和運行線程。 – dotslash 2014-11-22 17:19:47