我只是個begginers流,並嘗試學習Java讓我們得到在java中
目標 - 嘗試在Java創建幾流,我的程序一定要創建3個流和1個主流程,比停止。
請告訴我做:
與創建類實現Runnable接口
class NewThread implements Runnable {
String name;
Thread t;
NewThread(String threadname){
name = threadname;
t = new Thread (this, name);
System.out.println(t);
t.start();
}
public void run(){
try {
System.out.println("111");// why cant see it?
Thread.sleep(1000);
}
catch (InterruptedException e){
System.out.println(e);
}
System.out.println("End Thread");
}
而且主:
public class ThreadDemo {
public static void main (String []args){
new Thread ("F");
new Thread ("S");
new Thread ("T");
try {
Thread.sleep(10000);
}
catch (InterruptedException e){
}
System.out.println("End M");
}
}
我想我會得到像111 3串和一個字符串結束米 -
111
111
111
End M
,但我得到的只是
End M
誰能說爲什麼我沒有得到我的PROGRAMM結果3串?
非常感謝大家。
OP已經在'NewThread'構造函數中啓動了一個新的'Thread'。所以不需要在'main'中手動啓動'Thread'。 – 2013-03-04 19:33:05