1 // Various thread tests.
3 public class Thread_1 extends Thread
5 // The group for the workers.
6 static ThreadGroup subgroup;
8 // Which piece of test code to try.
11 // Names of the tests.
12 static final int JOIN_GOOD = 0;
13 static final int JOIN_TIMEOUT = 1;
14 static final int JOIN_INTERRUPTED = 2;
15 static final int THREAD_EXIT = 3;
17 // True if this is normal; false if daemon.
19 // The other thread in the test.
21 // True when the thread has entered run().
30 System.out.println ("test " + test_case);
31 // Tell the main thread to start the daemon thread.
37 // Now wait for daemon to start.
40 while (! other.started)
47 System.out.println ("joined");
51 System.out.println (other.isAlive());
54 case JOIN_INTERRUPTED:
56 System.out.println ("joined");
63 System.out.println ("failure");
69 // Let the normal thread start first.
72 while (! other.started)
75 // Tell normal thread that we've started.
84 System.out.println ("daemon done");
89 case JOIN_INTERRUPTED:
93 // Wait for a while. However, don't wait indefinitely
94 // -- we want this thread to terminate so that the
95 // process won't hang if there is a bug.
97 System.out.println ("daemon still alive");
101 System.out.println ("failure");
106 catch (InterruptedException e)
108 System.out.println ("interrupted");
112 public void setOther (Thread_1 x)
117 Thread_1 (String name, boolean x)
119 super (subgroup, name);
122 setDaemon (! normal);
125 // Run a single test.
126 static Thread_1 doit (int what)
128 // FIXME: we used to just use the same threads each time. That
129 // didn't work -- must debug.
130 Thread_1 dt = new Thread_1 ("daemon", false);
131 Thread_1 nt = new Thread_1 ("normal", true);
142 // Don't wait for the threads if we're doing the exit test.
143 if (what != THREAD_EXIT)
149 catch (InterruptedException e)
151 System.out.println ("caught bad exception");
157 public static void main (String[] args)
159 subgroup = new ThreadGroup ("sub");
163 System.out.println ("active count = " + subgroup.activeCount ());
165 Thread_1 dt = doit (JOIN_TIMEOUT);
166 // Make sure that joining a dead thread works.
167 System.out.println ("still alive: " + dt.isAlive ());
172 catch (InterruptedException e)
174 System.out.println ("exception caught");
177 doit (JOIN_INTERRUPTED);
179 // This test must come last.