OSDN Git Service

Initial revision
[pf3gnuchains/gcc-fork.git] / libjava / testsuite / libjava.lang / Thread_1.java
1 // Various thread tests.
2
3 public class Thread_1 extends Thread
4 {
5   // The group for the workers.
6   static ThreadGroup subgroup;
7
8   // Which piece of test code to try.
9   static int test_case;
10
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;
16
17   // True if this is normal; false if daemon.
18   boolean normal;
19   // The other thread in the test.
20   Thread_1 other;
21   // True when the thread has entered run().
22   boolean started;
23
24   public void run ()
25   {
26     try
27       {
28         if (normal)
29           {
30             System.out.println ("test " + test_case);
31             // Tell the main thread to start the daemon thread.
32             synchronized (this)
33               {
34                 started = true;
35                 notify ();
36               }
37             // Now wait for daemon to start.
38             synchronized (other)
39               {
40                 while (! other.started)
41                   other.wait ();
42               }
43             switch (test_case)
44               {
45               case JOIN_GOOD:
46                 other.join ();
47                 System.out.println ("joined");
48                 break;
49               case JOIN_TIMEOUT:
50                 other.join (10);
51                 System.out.println (other.isAlive());
52                 other.join ();
53                 break;
54               case JOIN_INTERRUPTED:
55                 other.join ();
56                 System.out.println ("joined");
57                 break;
58               case THREAD_EXIT:
59                 // Nothing.
60                 break;
61
62               default:
63                 System.out.println ("failure");
64                 break;
65               }
66           }
67         else
68           {
69             // Let the normal thread start first.
70             synchronized (other)
71               {
72                 while (! other.started)
73                   other.wait();
74               }
75             // Tell normal thread that we've started.
76             synchronized (this)
77               {
78                 started = true;
79                 notify ();
80               }
81             switch (test_case)
82               {
83               case JOIN_GOOD:
84                 System.out.println ("daemon done");
85                 break;
86               case JOIN_TIMEOUT:
87                 sleep (50);
88                 break;
89               case JOIN_INTERRUPTED:
90                 other.interrupt ();
91                 break;
92               case THREAD_EXIT:
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.
96                 sleep (10000);
97                 System.out.println ("daemon still alive");
98                 break;
99
100               default:
101                 System.out.println ("failure");
102                 break;
103               }
104           }
105       }
106     catch (InterruptedException e)
107       {
108         System.out.println ("interrupted");
109       }
110   }
111
112   public void setOther (Thread_1 x)
113   {
114     other = x;
115   }
116
117   Thread_1 (String name, boolean x)
118   {
119     super (subgroup, name);
120     normal = x;
121     started = false;
122     setDaemon (! normal);
123   }
124
125   // Run a single test.
126   static Thread_1 doit (int what)
127   {
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);
132
133     dt.setOther(nt);
134     nt.setOther(dt);
135
136     test_case = what;
137     try
138       {
139         nt.start();
140         dt.start();
141
142         // Don't wait for the threads if we're doing the exit test.
143         if (what != THREAD_EXIT)
144           {
145             nt.join ();
146             dt.join ();
147           }
148       }
149     catch (InterruptedException e)
150       {
151         System.out.println ("caught bad exception");
152       }
153
154     return dt;
155   }
156
157   public static void main (String[] args)
158   {
159     subgroup = new ThreadGroup ("sub");
160
161     doit (JOIN_GOOD);
162
163     System.out.println ("active count = " + subgroup.activeCount ());
164
165     Thread_1 dt = doit (JOIN_TIMEOUT);
166     // Make sure that joining a dead thread works.
167     System.out.println ("still alive: " + dt.isAlive ());
168     try
169       {
170         dt.join ();
171       }
172     catch (InterruptedException e)
173       {
174         System.out.println ("exception caught");
175       }
176
177     doit (JOIN_INTERRUPTED);
178
179     // This test must come last.
180     doit (THREAD_EXIT);
181   }
182 }