SuppressWarnings("deprecation") 对于 java 中的一行

SuppressWarnings("deprecation") for one line in java

我正在使用线程并使用 thread.stop();结束线程以临时解决我遇到的问题。我知道它已被弃用,不应使用,但如何仅针对该行抑制编译器警告?我想继续收到 class 其余部分的弃用警告,只是不是那一行。我尝试使用下面的代码并在 @SupressWarnings("deprecation") 行收到错误 "Annotations are not allowed here"。抑制此错误的正确方法是什么?

class Handeler {
   private Thread thread;
   .....
   void stopThread() {
      if(thread!=null && thread.isAlive()) {                 
         @SuppressWarnings("deprecation")
            thread.stop();
      }
   }
}

您可以改为注释该方法:

class Handeler {
   private Thread thread;
   .....
   @SuppressWarnings("deprecation")
   void stopThread() {
      if(thread!=null && thread.isAlive()) {                 
            thread.stop();
      }
   }
}

不允许单行注释。

您可以使用注释抑制警告:

void stopThread() {
      if(thread!=null && thread.isAlive()) {

          //noinspection deprecation
          thread.stop();
      }
}