Java基础知识总结(76)

1、今天学了什么

    1、线程停止

    (1)stop()

   

        stop()方法已被废弃,我们是不能随便中断一个线程的,我们无法知道这个线程正运行在什么状态,它可能持有某把锁,强行中断线程可能导致锁不能释放的问题;或者线程可能在操作数据库,强行中断线程可能导致数据不一致的问题。正是由于调用stop()方法来终止线程可能会产生不可预料的结果,因此不推荐调用stop()方法。

       

    (2)异常中断法

/**

 * 线程休眠后终止

   */

   public class TheadStopDemo1 {

   public static void main(String[] args) throws InterruptedException {

       Thread thread = new Thread(()->{

           //线程休眠10秒钟

           try {

               TimeUnit.SECONDS.sleep(10);

           } catch (InterruptedException e) {

               throw new RuntimeException(e);

           }

       

       });

       

       //线程启动

       thread.start();

       //主线程休眠3秒,确保线程thead顺利执行

       TimeUnit.SECONDS.sleep(3);

       //将运行中的线程设置中断标记

       thread.interrupt();

   }

   }


 

/**

 * 线程wait()后终止

   */

   public class TheadStopDemo2 {

   public static void main(String[] args) throws InterruptedException {

       Thread thread= new Thread(()->{

           //线程wait()

           System.out.println("线程thread进入等待状态");

           Object o = new Object();

           synchronized (o){

               try {

                   o.wait();

               } catch (InterruptedException e) {

                   System.out.println("线程终止");

                   throw new RuntimeException(e);

               }

           }

       });

       thread.start();

       TimeUnit.SECONDS.sleep(3);

       thread.interrupt();

   }

   }

/**

 * 通过中断标记终止线程

   */

   public class TheadStopDemo3 {

   public static void main(String[] args) throws InterruptedException {

       Thread t= new Thread(()->{

           Thread thread = Thread.currentThread();

           System.out.println("线程t的运行状态"+thread.getState());

               while (true){

                   System.out.println(true);

                   //判断是否存在暂停标志

                   if (thread.isInterrupted()){

                       System.out.println("线程终止!");

                       return;

                   }

               }

       });

       t.start();

       TimeUnit.SECONDS.sleep(3);

       //设置线程的终止标志

       t.interrupt();

   }

   }

    interrupt()和interrupted()的区别?

   

    1. interrupt()是实例方法,用于为正在运行的线程添加中断标记,当线程正在运行时,该方法并不会影响线程的运行,如果线程处于WAITING或者TIMED_WAITING状态时,调用该方法时,线程会抛出中断异常。

    2. interrupted()是类方法,用于检测线程是否有中断标记,如果线程有中断标记,调用该方法时会清除标记,清除标记后再次调用该方法会返回false,除非再次为线程添加中断标记


 

/**

 * Interrupt 和 Interrupted的区别

   */

   public class TheadStopDemo4 {

   public static void main(String[] args) throws InterruptedException {

       Thread t = new Thread(()->{

           Thread thread = Thread.currentThread();

           for (int i = 0; i < 100; i++) {

               if(i==20){

                   //设置线程中断标志

                   //interrupt() 为实例方法

                   thread.interrupt();

                   System.out.println(thread.getState());//RUNNABLE

                   //获取线程是否有中断标志

                   System.out.println(thread.isInterrupted());//true

                   //此时线程正处于运行状态,isInterrupted()不会改变运行状态

                   System.out.println(thread.getState());//RUNNABLE

                   System.out.println("------------------");

               }else if(i==40){

                   //中断线程 并消除中断标记 如果线程有标记返回ture 没有标记返回false

                   Thread.interrupted();

                   System.out.println(thread.getState());

                   System.out.println(thread.isInterrupted());//false

                   System.out.println("------------------");

               } else if (i==60) {

                   //再次标记

                   thread.interrupt();

                   System.out.println(thread.getState());

                   System.out.println(thread.isInterrupted());//true

                   System.out.println("------------------");

               } else if (i==80) {

                   Thread.interrupted();

                   System.out.println(thread.getState());

                   System.out.println(thread.isInterrupted());//false

                   System.out.println("------------------");

               }

           }

       });

       t.start();

       TimeUnit.SECONDS.sleep(3);

   }

   }

  • 19
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好教员好

您的鼓励是我前进动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值