aboutsummaryrefslogtreecommitdiff
path: root/libjava/testsuite/libjava.lang/Thread_Join.java
blob: 711b05cf0f8a415fb9edda60e0bfe58663388d8d (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
// Many threads join a single thread.
// Origin: Bryce McKinlay <bryce@albatross.co.nz>

class Sleeper implements Runnable
{
  int num = -1;
  
  public Sleeper(int num)
  {
    this.num = num;
  }
  
  public void run()
  {
    System.out.println("sleeping");
    try
    {
      Thread.sleep(500);
    }
    catch (InterruptedException x)
    {
      System.out.println("sleep() interrupted");
    }
    System.out.println("done");
  }
}

class Joiner implements Runnable
{
  Thread join_target;
  
  public Joiner(Thread t)
  {
    this.join_target = t;
  }
  
  public void run()
  {
    try
    {
      long start = System.currentTimeMillis();
      join_target.join(2000);
      if ((System.currentTimeMillis() - start) > 1900)
        System.out.println("Error: Join timed out");
      else
        System.out.println("ok");
    }
    catch (InterruptedException x)
    {
      System.out.println("join() interrupted");
    }
  }
  
}

public class Thread_Join
{
  public static void main(String[] args)
  {
    Thread primary = new Thread(new Sleeper(1));
    primary.start();
    for (int i=0; i < 10; i++)
    {
      Thread t = new Thread(new Joiner(primary));
      t.start();
    }
  }
}