OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [testsuite/] [libjava.lang/] [Thread_Wait_2.java] - Blame information for rev 765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 765 jeremybenn
// Create many threads waiting on a monitor. Interrupt some of them. Do the 
2
// others wake up correctly with notify() and/or notifyAll()?
3
 
4
import java.util.Vector;
5
 
6
class Waiter extends Thread
7
{
8
  Object monitor;
9
  int thread_num;
10
  boolean interrupted = false;
11
  boolean notified = false;
12
 
13
  Waiter (Object monitor, int thread_num)
14
  {
15
    this.monitor = monitor;
16
    this.thread_num = thread_num;
17
  }
18
 
19
  public void run()
20
  {
21
    synchronized (monitor)
22
      {
23
        try
24
        {
25
          monitor.wait();
26
          notified = true;
27
        }
28
        catch (InterruptedException x)
29
        {
30
          interrupted = true;
31
        }
32
      }
33
 
34
  }
35
}
36
 
37
public class Thread_Wait_2
38
{
39
  static Vector threads;
40
  static Object monitor = new Object();
41
 
42
  static final int NUM_THREADS = 10;
43
 
44
  public static void main(String args[])
45
  {
46
 
47
 
48
    try
49
    {
50
      makeThreads ();
51
 
52
      Thread.sleep(250);
53
 
54
      // Interrupt a few threads...
55
      Waiter i1 = (Waiter) threads.elementAt(3);
56
      Waiter i2 = (Waiter) threads.elementAt(4);
57
      Waiter i3 = (Waiter) threads.elementAt(9);
58
      i1.interrupt();
59
      i2.interrupt();
60
      i3.interrupt();
61
 
62
      // Call notify the exact number of times required to wake the remaining
63
      // threads.
64
      synchronized (monitor)
65
      {
66
        for (int i=0; i < NUM_THREADS -3 ; i++)
67
        {
68
          monitor.notify ();
69
        }
70
      }
71
 
72
      joinAll();
73
      printStatus();
74
 
75
      // Repeat all the above, but use notifyAll() instead.
76
      makeThreads();
77
 
78
      Thread.sleep(250);
79
 
80
      // Interrupt a few threads...
81
      i1 = (Waiter) threads.elementAt(0);
82
      i2 = (Waiter) threads.elementAt(1);
83
      i3 = (Waiter) threads.elementAt(9);
84
      i1.interrupt();
85
      i2.interrupt();
86
      i3.interrupt();
87
 
88
      // Call notifyAll to wake the remaining threads.
89
      synchronized (monitor)
90
      {
91
        monitor.notifyAll ();
92
      }
93
 
94
      joinAll();
95
      printStatus();
96
 
97
    }
98
    catch (InterruptedException x)
99
    {
100
      System.out.println (x);
101
    }
102
 
103
 
104
  }
105
 
106
  static void makeThreads()
107
  {
108
    threads = new Vector(NUM_THREADS);
109
 
110
    for (int i=0; i < NUM_THREADS; i++)
111
      {
112
        Waiter w = new Waiter(monitor, i);
113
        w.start();
114
        threads.addElement(w);
115
      }
116
  }
117
 
118
  static void joinAll()
119
  {
120
    try
121
    {
122
      for (int i=0; i < threads.size(); i++)
123
        {
124
          Thread t = (Thread) threads.elementAt(i);
125
          t.join();
126
        }
127
    }
128
    catch (InterruptedException x) {}
129
  }
130
 
131
  static void printStatus()
132
  {
133
    for (int i=0; i < threads.size(); i++)
134
      {
135
        Waiter w = (Waiter) threads.elementAt(i);
136
        if (w.interrupted)
137
          System.out.println (i + " interrupted.");
138
        if (w.notified)
139
          System.out.println (i + " notified.");
140
      }
141
  }
142
 
143
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.