1 | // Test interrupt() behaviour on a thread in wait(), sleep(), and spinning
|
---|
2 | // in a loop.
|
---|
3 | // Origin: Bryce McKinlay <bryce@albatross.co.nz>
|
---|
4 |
|
---|
5 | class Waiter extends Thread
|
---|
6 | {
|
---|
7 | public synchronized void run()
|
---|
8 | {
|
---|
9 | System.out.println ("wait()");
|
---|
10 | try
|
---|
11 | {
|
---|
12 | wait();
|
---|
13 | System.out.println("Error: wait() completed normally.");
|
---|
14 | }
|
---|
15 | catch (InterruptedException x)
|
---|
16 | {
|
---|
17 | if (isInterrupted() || interrupted())
|
---|
18 | System.out.println("Error: interrupt flag is still set.");
|
---|
19 |
|
---|
20 | }
|
---|
21 | System.out.println("interrupted - ok");
|
---|
22 | }
|
---|
23 | }
|
---|
24 |
|
---|
25 | class Sleeper extends Thread
|
---|
26 | {
|
---|
27 | public void run()
|
---|
28 | {
|
---|
29 | System.out.println ("sleep()");
|
---|
30 | try
|
---|
31 | {
|
---|
32 | sleep(2000);
|
---|
33 | System.out.println("Error: sleep() completed normally.");
|
---|
34 | }
|
---|
35 | catch (InterruptedException x)
|
---|
36 | {
|
---|
37 | if (isInterrupted() || interrupted())
|
---|
38 | System.out.println("Error: interrupt flag is still set.");
|
---|
39 |
|
---|
40 | System.out.println("interrupted - ok");
|
---|
41 | }
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | class Looper extends Thread
|
---|
46 | {
|
---|
47 | // Return the number of Thread.yield()s we can do in 500ms.
|
---|
48 | static long calibrate ()
|
---|
49 | {
|
---|
50 | long i = 1;
|
---|
51 |
|
---|
52 | for (int tries = 0; tries < 40; tries++)
|
---|
53 | {
|
---|
54 | long t = System.currentTimeMillis();
|
---|
55 | for (long n = 0; n < i; n++)
|
---|
56 | Thread.yield();
|
---|
57 | long t_prime = System.currentTimeMillis();
|
---|
58 | if (t_prime - t > 500)
|
---|
59 | return i;
|
---|
60 | i *= 2;
|
---|
61 | }
|
---|
62 | // We have no system clock. Give up.
|
---|
63 | throw new RuntimeException ("We have no system clock.");
|
---|
64 | }
|
---|
65 |
|
---|
66 | static long yields = calibrate ();
|
---|
67 |
|
---|
68 | public void run()
|
---|
69 | {
|
---|
70 | System.out.println ("Busy waiting");
|
---|
71 |
|
---|
72 | int count = 0;
|
---|
73 | for (long i=0; i < yields; i++)
|
---|
74 | {
|
---|
75 | Thread.yield();
|
---|
76 | count += 5;
|
---|
77 | if (isInterrupted ())
|
---|
78 | break;
|
---|
79 | }
|
---|
80 | synchronized (this)
|
---|
81 | {
|
---|
82 | if (interrupted ())
|
---|
83 | {
|
---|
84 | System.out.println ("interrupted - ok");
|
---|
85 | if (isInterrupted () || interrupted ())
|
---|
86 | System.out.println("Error: interrupt flag is still set.");
|
---|
87 | }
|
---|
88 | else
|
---|
89 | System.out.println ("Error: Busy wait was not interrupted.");
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | class Joiner extends Thread
|
---|
95 | {
|
---|
96 | public void run()
|
---|
97 | {
|
---|
98 | System.out.println("join()");
|
---|
99 | try
|
---|
100 | {
|
---|
101 | join(2000);
|
---|
102 | System.out.println("Error: join() completed normally??!");
|
---|
103 | }
|
---|
104 | catch (InterruptedException x)
|
---|
105 | {
|
---|
106 | if (isInterrupted() || interrupted())
|
---|
107 | System.out.println("Error: interrupt flag is still set.");
|
---|
108 |
|
---|
109 | System.out.println("interrupted - ok");
|
---|
110 | }
|
---|
111 |
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | public class Thread_Interrupt
|
---|
116 | {
|
---|
117 | public static void main(String args[])
|
---|
118 | {
|
---|
119 | Waiter w = new Waiter();
|
---|
120 | w.start ();
|
---|
121 | sleep_and_interrupt (w);
|
---|
122 |
|
---|
123 | Sleeper s = new Sleeper();
|
---|
124 | s.start ();
|
---|
125 | sleep_and_interrupt (s);
|
---|
126 |
|
---|
127 | Looper l = new Looper ();
|
---|
128 | l.start ();
|
---|
129 | sleep_and_interrupt (l);
|
---|
130 |
|
---|
131 | Joiner j = new Joiner ();
|
---|
132 | j.start ();
|
---|
133 | sleep_and_interrupt (j);
|
---|
134 | }
|
---|
135 |
|
---|
136 | public static void sleep_and_interrupt(Thread t)
|
---|
137 | {
|
---|
138 | try
|
---|
139 | {
|
---|
140 | Thread.sleep (250);
|
---|
141 | t.interrupt ();
|
---|
142 | long t1 = System.currentTimeMillis();
|
---|
143 | t.join (5000);
|
---|
144 | long time = System.currentTimeMillis() - t1;
|
---|
145 | if (time > 2900)
|
---|
146 | {
|
---|
147 | System.out.println ("Error: join() from main thread timed out");
|
---|
148 | }
|
---|
149 | }
|
---|
150 | catch (InterruptedException x)
|
---|
151 | {
|
---|
152 | System.out.println("Error: main thread interrupted.");
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|