我認爲對象只存在於已定義的範圍內。但是在這個程序中,在執行thtest
方法之後,t1
線程的對象仍然存在,我可以在輸出中看到「hi」和「bye」。爲什麼線程對象在範圍之後不被銷燬?
public class apples {
public static void main(String args[]){
int b =1;
navid n = new navid();
n.thtest();
while (b==1){ System.out.println("bye"); }
}
}
public class navid {
int a=1;
public void thtest(){
Runnable te = new Runnable() {
public void run(){
try{
while (a==1){ System.out.println("hi");}
}catch(Exception e){}
}
};
Thread t1 = new Thread (te);
t1.start();
}
}
你知道'Thread'是什麼嗎? – BackSlash
它的工作按照你編碼。 – Kick
執行的路徑? – user2555974