我在 java 中启动线程,我想清楚地了解start()
/run()
在我的情况下的行为。
我创建了一个线程 calle t,然后放置t.start()
了一个 for 循环。
for 循环是 thread.t 的一部分还是主线程的一部分?
class Job implements Runnable{
Thread t;
Job(String tName){
t=new Thread(this, tName);
System.out.println("This is thread: "+t.getName());
t.start();
System.out.println("This is the end of constructor");
try{ /*<<<<<<<------------WILL THIS RUN in my MAIN thread or in the t thread??*/
for(int i=0;i<5;i++){
System.out.println("xxxThis is the count i value: "+i+" "+ t.getName());
Thread.sleep(1000);
}
}
catch(InterruptedException e){
System.out.println("The thread has been interrupted");
}
}
public void run(){
System.out.println("This is the start of RUN()");
try{
for(int i=0;i<5;i++){
System.out.println("This is the count i value: "+i+" "+ t.getName());
Thread.sleep(1000);
}
}
catch(InterruptedException e){
System.out.println("The thread has been interrupted");
}
finally{
System.out.println("Fnally block reached: "+t.getName());
}
}
}