【小知识探究系列三】探究stop、destroy、suspend、resume被淘汰的原因

开头小诗一首以歌颂留在历史长河的她们:
《题都城南庄》 崔护
去年今日此门中,人面桃花相映红。人面不知何处去,桃花依旧笑春风。
醒醒,我这是在写技术文章,怎么开始吟诗作赋了,罪过。【废话到此为止】


我们知道像stop、destroy、suspend、resume这几种中断或者阻塞线程的方法在较高java版本中已经被标记上了@Deprecated过期标签,那么为什么她们曾经登上了java的历史舞台而又渐渐的推出了舞台呢,到底是人性的扭曲还是道德的沦丧呢,亦或是她们不思进取被取而代之呢,如果是被取而代之,那么取而代之的又是何方人也,本文我们将一探究竟。

一、stop的落幕
首先stop方法的作用是什么呢,用java源码中的一句注释来了解一下:Forces the thread to stop executing.,即强制线程停止执行,'Forces’似乎已经透漏出了stop方法的蛮狠无理。那么我们再看看java开发者是怎们解释stop被淘汰了的:

This method is inherently unsafe. Stopping a thread with Thread.stop causes it to unlock all of the monitors that it has locked (as a natural consequence of the uncheckedThreadDeath exception propagating up the stack). If any of the objects previously protected by these monitors were in an inconsistent state, the damaged objects become visible to other threads, potentially resulting in arbitrary behavior. Many uses of stop should be replaced by code that simply modifies some variable to indicate that the target thread should stop running. The target thread should check this variable regularly, and return from its run method in an orderly fashion if the variable indicates that it is to stop running. If the target thread waits for long periods (on a condition variable, for example), the interrupt method should be used to interrupt the wait.

我们从中可以看出以下几点:
1.stop这种方法本质上是不安全的
2.使用Thread.stop停止线程会导致它解锁所有已锁定的监视器,即直接释放当前线程已经获取到的所有锁,是当前线程直接进入阻塞状态
我们举例来看一下上边提到的两点:


public static void main(String[] args) throws InterruptedException {
   
   
        Object o1=new Object();
        Object o2=new Object();
        Thread t1=new Thread(()->{
   
   
              synchronized (o1)
              {
   
   
                  synchronized (o2)
                  {
   
   
                      try {
   
   
                          System.out.println("t1获取到锁");
                          Thread.sleep(5000);
                          System.out.println("t1结束");
                      } catch (InterruptedException e) {
   
   
                          e.printStackTrace();
                      }
                  }
              }
        });
        t1.start();
        Thread.sleep(1000);
        Thread t2=new Thread(()->{
   
   
            synchronized (o1)
            {
   
   
                synchronized (o2)
                {
   
   
                    try {
   
   
                        System.out.println("t2获取到锁");
                        Thread.sleep(5000);
                        System.out.println("t2结束");
                    } catch (InterruptedException e) {
   
   
                        e.printStackTrace();
                    }
                }
            }
        });
        t2.start();
        t1.stop();
    }

运行结果:
在这里插入图片描述
可以看到,当线程t1在获取到o1和o2两个锁开始执行,在还没有执行结束的时候,主线程调用了t1的stop方法中断了t1的执行,释放了t1线程获取到的所有锁,中断后t2获取到了o1和o2锁,开始执行直到结束,而t1却夭折在了sleep的时候,sleep后的代码没有执行。如果sleep后的代码是资源释放、重要业务逻辑等比较重要的代码的话,亦或是其他线程依赖t1线程的运行结果,那直接中断将可能造成很严重的后果。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章