package test41;
import java.util.Date;
/***
* public final void stop():让线程停止,过时了,但是还是可以使用的,不过太过于暴力,存在安全隐患
* public void interrupt():中断进程,把线程的状态终止,并抛出一个InterruptedException
* @author samsung
*
*/
public class Threadstop {
public static void main(String[] args) {
Threadsp s1 = new Threadsp();
s1.start();
try {
Thread.sleep(3000);
// s1.stop();
s1.interrupt();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Threadsp extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("开始执行:" + new Date());
// 睡眠10秒钟
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("进程被结束了");
}
System.out.println("结束执行:" + new Date());
}
}
线程的中断
最新推荐文章于 2023-06-26 23:29:57 发布