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

Subversion Repositories openrisc

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 765 jeremybenn
// Test interrupt() behaviour on a thread in wait(), sleep(), and spinning 
2
// in a loop.
3
 
4
class ThreadBase extends Thread
5
{
6
  boolean ready = false;
7
 
8
  synchronized void ready()
9
  {
10
    ready = true;
11
  }
12
}
13
 
14
class Waiter extends ThreadBase
15
{
16
  public synchronized void run()
17
  {
18
    super.ready();
19
    System.out.println ("wait()");
20
    try
21
    {
22
      wait();
23
      System.out.println("Error: wait() completed normally.");
24
    }
25
    catch (InterruptedException x)
26
    {
27
      if (isInterrupted() || interrupted())
28
        System.out.println("Error: interrupt flag is still set.");
29
 
30
    }
31
      System.out.println("interrupted - ok");
32
  }
33
}
34
 
35
class Sleeper extends ThreadBase
36
{
37
  public void run()
38
  {
39
    super.ready();
40
    System.out.println ("sleep()");
41
    try
42
    {
43
      sleep(5000);
44
      System.out.println("Error: sleep() completed normally.");
45
    }
46
    catch (InterruptedException x)
47
    {
48
      if (isInterrupted() || interrupted())
49
        System.out.println("Error: interrupt flag is still set.");
50
 
51
      System.out.println("interrupted - ok");
52
    }
53
  }
54
}
55
 
56
class Looper extends ThreadBase
57
{
58
  public void run()
59
  {
60
    super.ready();
61
    System.out.println ("Busy waiting");
62
 
63
    int count = 0;
64
    long start = System.currentTimeMillis();
65
    while (true)
66
      {
67
        Thread.yield();
68
        if (isInterrupted ())
69
          break;
70
        long now = System.currentTimeMillis();
71
        if ((now - start) > 5000)
72
          break;
73
      }
74
    synchronized (this)
75
    {
76
      if (interrupted ())
77
        {
78
          System.out.println ("interrupted - ok");
79
          if (isInterrupted () || interrupted ())
80
            System.out.println("Error: interrupt flag is still set.");
81
        }
82
      else
83
        System.out.println ("Error: Busy wait was not interrupted.");
84
    }
85
  }
86
}
87
 
88
class Joiner extends ThreadBase
89
{
90
  public void run()
91
  {
92
    super.ready();
93
    System.out.println("join()");
94
    try
95
    {
96
      join(2000);
97
      System.out.println("Error: join() completed normally??!");
98
    }
99
    catch (InterruptedException x)
100
    {
101
      if (isInterrupted() || interrupted())
102
        System.out.println("Error: interrupt flag is still set.");
103
 
104
      System.out.println("interrupted - ok");
105
    }
106
 
107
  }
108
}
109
 
110
public class Thread_Interrupt
111
{
112
  public static void main(String args[])
113
  {
114
    Waiter w = new Waiter();
115
    w.start ();
116
    sleep_and_interrupt (w);
117
 
118
    Sleeper s = new Sleeper();
119
    s.start ();
120
    sleep_and_interrupt (s);
121
 
122
    Looper l = new Looper ();
123
    l.start ();
124
    sleep_and_interrupt (l);
125
 
126
    Joiner j = new Joiner ();
127
    j.start ();
128
    sleep_and_interrupt (j);
129
  }
130
 
131
  public static void sleep_and_interrupt(ThreadBase t)
132
  {
133
    try
134
    {
135
      synchronized (t)
136
        {
137
          while (!t.ready)
138
            t.wait(10);
139
        }
140
 
141
      Thread.sleep (50);
142
      t.interrupt ();
143
      long t1 = System.currentTimeMillis();
144
      t.join (5000);
145
      long time = System.currentTimeMillis() - t1;
146
      if (time > 2900)
147
        {
148
          System.out.println ("Error: join() from main thread timed out");
149
        }
150
    }
151
    catch (InterruptedException x)
152
    {
153
      System.out.println("Error: main thread interrupted.");
154
    }
155
  }
156
}

powered by: WebSVN 2.1.0

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