线程的生命周期是什么 java(线程的整个生命周期分为五个阶段)
大家好,我是编辑老虎油,我来为大家解答以上问题。什么是线程的生命周周期?很多人还不知道线程的生命周期间。现在让我们来看看!
本文到此结束。希望对你有帮助。
//这是线程被中断且具有相同生命周期的代码。希望能帮到你!
类MyThread实现Runnable {
@覆盖
公共void运行(){
system . out . println(“1,输入run()方法休眠“);
尝试{
system . out . println(“2,线程休眠20秒“);
thread . sleep(20000);//在这里睡20秒
system . out . println(“3。线程正常休眠“);
} catch(中断异常e ){
system . out . println(“4。线程因异常休眠而中断“);
返回;//返回到方法调用。
}
system . out . println(“5。线程正常结束时运行()方法体“);
}
}
公共类中断演示{
公共静态void main(String[] args) {
MyThread mt = new MyThread();
线程t =新线程(mt,“线程a”);
t . start();//启动线程
//========================================================
尝试{
thread . sleep(2000);//确保线程至少执行2秒钟。
} catch(中断异常e ){
e . printstacktrace();
}
//========================================================
t . interrupt();//中断线程
}
}