View Javadoc
1   import annotations.DetectorUnderTest;
2   import edu.umd.cs.findbugs.annotations.DesireWarning;
3   import edu.umd.cs.findbugs.annotations.ExpectWarning;
4   import edu.umd.cs.findbugs.annotations.NoWarning;
5   import edu.umd.cs.findbugs.detect.SuspiciousThreadInterrupted;
6
7   @DetectorUnderTest(SuspiciousThreadInterrupted.class)
8   public class UselessCurrentThread implements Runnable {
9       private Thread unknownThread;
10
11      public UselessCurrentThread(Thread t) {
12          unknownThread = t;
13      }
14
15      @NoWarning("STI_INTERRUPTED_ON_CURRENTTHREAD")
16      void test1() throws InterruptedException {
17          while (!Thread.interrupted()) {
18              System.out.println("huh?");
19              Thread.sleep(10000);
20          }
21      }
22
23      void test2() throws InterruptedException {
24          Thread.currentThread();
25          while (!Thread.interrupted()) {
26              System.out.println("huh?");
27              Thread.sleep(10000);
28          }
29      }
30
31      @ExpectWarning("STI_INTERRUPTED_ON_CURRENTTHREAD")
32      void test3() throws InterruptedException {
33          while (!Thread.currentThread().interrupted()) {
34              System.out.println("huh?");
35              Thread.sleep(10000);
36          }
37      }
38
39      // XXX no warning with ecj (Eclipse) compiler (bad)
40      @DesireWarning("STI_INTERRUPTED_ON_UNKNOWNTHREAD")
41      void test4() throws InterruptedException {
42          Thread t = Thread.currentThread();
43          while (!unknownThread.interrupted()) {
44              System.out.println("huh?");
45              Thread.sleep(10000);
46          }
47      }
48
49      // XXX no warning with ecj (Eclipse) compiler (bad)
50      @DesireWarning("STI_INTERRUPTED_ON_UNKNOWNTHREAD")
51      void test5() throws InterruptedException {
52          while (!unknownThread.interrupted()) {
53              System.out.println("huh?");
54              Thread.sleep(10000);
55          }
56      }
57
58      @Override
59      public void run() {
60          try {
61
62              test1();
63              test2();
64              test3();
65              test4();
66
67          } catch (InterruptedException ie) {
68              System.out.println("Oh, ok");
69          }
70      }
71  }