Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / testsuite / libjava.lang / Thread_Interrupt.java
bloba69247422fea14e1ca5bd40c3639bcb1d9afe21b
1 // Test interrupt() behaviour on a thread in wait(), sleep(), and spinning
2 // in a loop.
4 class ThreadBase extends Thread
6 boolean ready = false;
8 synchronized void ready()
10 ready = true;
14 class Waiter extends ThreadBase
16 public synchronized void run()
18 super.ready();
19 System.out.println ("wait()");
20 try
22 wait();
23 System.out.println("Error: wait() completed normally.");
25 catch (InterruptedException x)
27 if (isInterrupted() || interrupted())
28 System.out.println("Error: interrupt flag is still set.");
31 System.out.println("interrupted - ok");
35 class Sleeper extends ThreadBase
37 public void run()
39 super.ready();
40 System.out.println ("sleep()");
41 try
43 sleep(5000);
44 System.out.println("Error: sleep() completed normally.");
46 catch (InterruptedException x)
48 if (isInterrupted() || interrupted())
49 System.out.println("Error: interrupt flag is still set.");
51 System.out.println("interrupted - ok");
56 class Looper extends ThreadBase
58 public void run()
60 super.ready();
61 System.out.println ("Busy waiting");
63 int count = 0;
64 long start = System.currentTimeMillis();
65 while (true)
67 Thread.yield();
68 if (isInterrupted ())
69 break;
70 long now = System.currentTimeMillis();
71 if ((now - start) > 5000)
72 break;
74 synchronized (this)
76 if (interrupted ())
78 System.out.println ("interrupted - ok");
79 if (isInterrupted () || interrupted ())
80 System.out.println("Error: interrupt flag is still set.");
82 else
83 System.out.println ("Error: Busy wait was not interrupted.");
88 class Joiner extends ThreadBase
90 public void run()
92 super.ready();
93 System.out.println("join()");
94 try
96 join(2000);
97 System.out.println("Error: join() completed normally??!");
99 catch (InterruptedException x)
101 if (isInterrupted() || interrupted())
102 System.out.println("Error: interrupt flag is still set.");
104 System.out.println("interrupted - ok");
110 public class Thread_Interrupt
112 public static void main(String args[])
114 Waiter w = new Waiter();
115 w.start ();
116 sleep_and_interrupt (w);
118 Sleeper s = new Sleeper();
119 s.start ();
120 sleep_and_interrupt (s);
122 Looper l = new Looper ();
123 l.start ();
124 sleep_and_interrupt (l);
126 Joiner j = new Joiner ();
127 j.start ();
128 sleep_and_interrupt (j);
131 public static void sleep_and_interrupt(ThreadBase t)
135 synchronized (t)
137 while (!t.ready)
138 t.wait(10);
141 Thread.sleep (50);
142 t.interrupt ();
143 long t1 = System.currentTimeMillis();
144 t.join (5000);
145 long time = System.currentTimeMillis() - t1;
146 if (time > 2900)
148 System.out.println ("Error: join() from main thread timed out");
151 catch (InterruptedException x)
153 System.out.println("Error: main thread interrupted.");