當我通過t.start()
調用run()
,但通過obj.start()
得不到相同的輸出時,我正在輸出輸出。可能是什麼原因?爲什麼start()在我的擴展線程中不起作用
任何人都可以解釋這一點嗎?
class Ex1 implements Runnable {
int n;
String s;
public void run(){
for(int i=1;i<=n;i++){
System.out.println(s +"-->"+ i);
}
}
}
class RunnableDemo extends Thread {
Ex1 e;
RunnableDemo(Ex1 e) {
this.e = e;
}
public static void main(String[] args) {
Ex1 obj1 = new Ex1();
Ex1 obj2 = new Ex1();
obj1.n = 5;
obj1.s = "abc";
// Thread t = new Thread(obj1);
// t.start();
RunnableDemo obj = new RunnableDemo(obj1);
obj.start();
}
}