aboutsummaryrefslogtreecommitdiff
path: root/libjava/testsuite/libjava.lang/Thread_1.java
blob: 42f8c840000749576e1911adb698488741b5431a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Various thread tests.

public class Thread_1 extends Thread
{
  // The group for the workers.
  static ThreadGroup subgroup;

  // Which piece of test code to try.
  static int test_case;

  // Names of the tests.
  static final int JOIN_GOOD = 0;
  static final int JOIN_TIMEOUT = 1;
  static final int JOIN_INTERRUPTED = 2;
  static final int THREAD_EXIT = 3;

  // True if this is normal; false if daemon.
  boolean normal;
  // The other thread in the test.
  Thread_1 other;
  // True when the thread has entered run().
  boolean started;

  public void run ()
  {
    try
      {
	if (normal)
	  {
	    System.out.println ("test " + test_case);
	    // Tell the main thread to start the daemon thread.
	    synchronized (this)
	      {
		started = true;
		notify ();
	      }
	    // Now wait for daemon to start.
	    synchronized (other)
	      {
		while (! other.started)
		  other.wait ();
	      }
	    switch (test_case)
	      {
	      case JOIN_GOOD:
		other.join ();
		System.out.println ("joined");
		break;
	      case JOIN_TIMEOUT:
		other.join (10);
		System.out.println (other.isAlive());
		other.join ();
		break;
	      case JOIN_INTERRUPTED:
		other.join ();
		System.out.println ("joined");
		break;
	      case THREAD_EXIT:
		// Nothing.
		break;

	      default:
		System.out.println ("failure");
		break;
	      }
	  }
	else
	  {
	    // Let the normal thread start first.
	    synchronized (other)
	      {
		while (! other.started)
		  other.wait();
	      }
	    // Tell normal thread that we've started.
	    synchronized (this)
	      {
		started = true;
		notify ();
	      }
	    switch (test_case)
	      {
	      case JOIN_GOOD:
		System.out.println ("daemon done");
		break;
	      case JOIN_TIMEOUT:
		sleep (50);
		break;
	      case JOIN_INTERRUPTED:
		other.interrupt ();
		break;
	      case THREAD_EXIT:
		// Wait for a while.  However, don't wait indefinitely
		// -- we want this thread to terminate so that the
		// process won't hang if there is a bug.
		sleep (10000);
		System.out.println ("daemon still alive");
		break;

	      default:
		System.out.println ("failure");
		break;
	      }
	  }
      }
    catch (InterruptedException e)
      {
	System.out.println ("interrupted");
      }
  }

  public void setOther (Thread_1 x)
  {
    other = x;
  }

  Thread_1 (String name, boolean x)
  {
    super (subgroup, name);
    normal = x;
    started = false;
    setDaemon (! normal);
  }

  // Run a single test.
  static Thread_1 doit (int what)
  {
    // FIXME: we used to just use the same threads each time.  That
    // didn't work -- must debug.
    Thread_1 dt = new Thread_1 ("daemon", false);
    Thread_1 nt = new Thread_1 ("normal", true);

    dt.setOther(nt);
    nt.setOther(dt);

    test_case = what;
    try
      {
	nt.start();
	dt.start();

	// Don't wait for the threads if we're doing the exit test.
	if (what != THREAD_EXIT)
	  {
	    nt.join ();
	    dt.join ();
	  }
      }
    catch (InterruptedException e)
      {
	System.out.println ("caught bad exception");
      }

    return dt;
  }

  public static void main (String[] args)
  {
    subgroup = new ThreadGroup ("sub");

    doit (JOIN_GOOD);

    System.out.println ("active count = " + subgroup.activeCount ());

    Thread_1 dt = doit (JOIN_TIMEOUT);
    // Make sure that joining a dead thread works.
    System.out.println ("still alive: " + dt.isAlive ());
    try
      {
	dt.join ();
      }
    catch (InterruptedException e)
      {
	System.out.println ("exception caught");
      }

    doit (JOIN_INTERRUPTED);

    // Note: this test has a race conditoin.  So we don't run it any
    // more.
    // This test must come last.
    // doit (THREAD_EXIT);
  }
}