我认为对象只存在于已定义的范围内。但是在这个程序中,在执行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