编程技术分享平台

网站首页 > 技术教程 正文

线程的正确终止:interrupt(线程中断的几种 方式)

xnh888 2024-10-30 04:45:16 技术教程 24 ℃ 0 评论


/**

interrupt()方法和stop()方法

void interrupt() 终止线程睡眠

*/

public class DemoInterrupt {

public static void main(String[] args) {

Thread t = new Thread(new MyRunnable2());

t.setName("t");

t.start();

try {

Thread.sleep(1000 * 5);

} catch (InterruptedException e) {

e.printStackTrace();

}

//终断t线程的睡眠(这种终断睡眠的方式依靠了java的异常处理机制。)

// t.interrupt();

t.stop(); //强行终止线程

//缺点:容易损坏数据 线程没有保存的数据容易丢失

}

}

class MyRunnable2 implements Runnable {

@Override

public void run() {

System.out.println(Thread.currentThread().getName() + "---> begin");

try {

//睡眠1年

Thread.sleep(1000 * 60 * 60 * 24 * 365);

} catch (InterruptedException e) {

// e.printStackTrace();

}

//1年之后才会执行这里

System.out.println(Thread.currentThread().getName() + "---> end");


}

}

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表