[英]How to add a lifecycle to a Thread that starts with "new Thread(new Runnable)..."?
Most examples of lifecycles for a Thread tend to be about threads in which you first instantiate them some way:线程生命周期的大多数示例往往是关于您首先以某种方式实例化它们的线程:
Thread t = new Thread();线程 t = new Thread();
Then you can refer to that thread in the lifecycle by saying stuff like:然后,您可以通过说以下内容来引用生命周期中的该线程:
t.join(); t.join();
or something like that.或类似的东西。
But how to do add the proper lifecyle when you have not physically instantiated the Thread, such as you start: new Thread(new Runnable)...但是当你没有物理实例化线程时如何添加适当的生命周期,比如你开始:new Thread(new Runnable)...
public static void main(String[] args) {
System.out.println("Before instantiation of thread");
new Thread(new Runnable(){
@Override
public void run() {
System.out.println("Thread State ::" + Thread.currentThread().getState());
for(int i=0;i<100;i++){
try {
Thread.sleep(1000);
System.out.println("Thread State ::" +i);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Thread State ::" + Thread.currentThread().getState());
}
}
}).start();
System.out.println("After instantiation of thread");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.