aboutsummaryrefslogtreecommitdiff
path: root/libjava/testsuite/libjava.lang/Thread_Interrupt.java
blob: a69247422fea14e1ca5bd40c3639bcb1d9afe21b (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
// Test interrupt() behaviour on a thread in wait(), sleep(), and spinning 
// in a loop.

class ThreadBase extends Thread
{
  boolean ready = false;
  
  synchronized void ready()
  {
    ready = true;
  }
}

class Waiter extends ThreadBase
{
  public synchronized void run()
  {
    super.ready();
    System.out.println ("wait()");
    try
    {
      wait();
      System.out.println("Error: wait() completed normally.");
    }
    catch (InterruptedException x)
    {
      if (isInterrupted() || interrupted()) 
        System.out.println("Error: interrupt flag is still set.");
    
    }
      System.out.println("interrupted - ok");
  }
}

class Sleeper extends ThreadBase
{
  public void run()
  {
    super.ready();
    System.out.println ("sleep()");
    try
    {
      sleep(5000);
      System.out.println("Error: sleep() completed normally.");
    }
    catch (InterruptedException x)
    {
      if (isInterrupted() || interrupted()) 
        System.out.println("Error: interrupt flag is still set.");
    
      System.out.println("interrupted - ok");
    }
  }
}

class Looper extends ThreadBase
{
  public void run()
  {
    super.ready();
    System.out.println ("Busy waiting");

    int count = 0;
    long start = System.currentTimeMillis();
    while (true)
      {
        Thread.yield();
	if (isInterrupted ())
	  break;
	long now = System.currentTimeMillis();	
	if ((now - start) > 5000)
	  break;
      }
    synchronized (this)
    {  
      if (interrupted ())
	{
	  System.out.println ("interrupted - ok");
	  if (isInterrupted () || interrupted ())
	    System.out.println("Error: interrupt flag is still set.");
	}
      else
	System.out.println ("Error: Busy wait was not interrupted.");          
    }
  }
}

class Joiner extends ThreadBase
{
  public void run()
  {
    super.ready();
    System.out.println("join()");
    try
    {
      join(2000);
      System.out.println("Error: join() completed normally??!");
    }
    catch (InterruptedException x)
    {
      if (isInterrupted() || interrupted()) 
        System.out.println("Error: interrupt flag is still set.");
    
      System.out.println("interrupted - ok");
    }

  }
}

public class Thread_Interrupt
{
  public static void main(String args[])
  {
    Waiter w = new Waiter();
    w.start ();
    sleep_and_interrupt (w);
    
    Sleeper s = new Sleeper();
    s.start ();
    sleep_and_interrupt (s);

    Looper l = new Looper ();
    l.start ();
    sleep_and_interrupt (l);

    Joiner j = new Joiner ();
    j.start ();
    sleep_and_interrupt (j);
  }
  
  public static void sleep_and_interrupt(ThreadBase t)
  {
    try
    {
      synchronized (t)
        {
	  while (!t.ready)
	    t.wait(10);
	}
    
      Thread.sleep (50);
      t.interrupt ();
      long t1 = System.currentTimeMillis();
      t.join (5000);
      long time = System.currentTimeMillis() - t1;
      if (time > 2900)
        {
	  System.out.println ("Error: join() from main thread timed out");
	}
    }
    catch (InterruptedException x)
    {
      System.out.println("Error: main thread interrupted.");
    }
  }
}