当前位置: 首页>后端>正文

线程的正确终止:interrupt

线程的正确终止:interrupt,第1张

/**

interrupt()方法和stop()方法

?void interrupt() 终止ass="superseo">线程睡眠

?*/

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");

????}

}


https://www.xamrdz.com/backend/3zr1923387.html

相关文章: