1 // Many threads join a single thread.
2 // Origin: Bryce McKinlay <bryce@albatross.co.nz>
4 class Sleeper
implements Runnable
8 public Sleeper(int num
)
15 System
.out
.println("sleeping");
20 catch (InterruptedException x
)
22 System
.out
.println("sleep() interrupted");
24 System
.out
.println("done");
28 class Joiner
implements Runnable
32 public Joiner(Thread t
)
41 long start
= System
.currentTimeMillis();
42 join_target
.join(2000);
43 if ((System
.currentTimeMillis() - start
) > 1900)
44 System
.out
.println("Error: Join timed out");
46 System
.out
.println("ok");
48 catch (InterruptedException x
)
50 System
.out
.println("join() interrupted");
56 public class Thread_Join
58 public static void main(String
[] args
)
60 Thread primary
= new Thread(new Sleeper(1));
62 for (int i
=0; i
< 10; i
++)
64 Thread t
= new Thread(new Joiner(primary
));