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
|
package com.nocandysw;
import java.util.concurrent.CountDownLatch;
import static java.lang.System.out;
public final class Locks {
private static final Object thing = new Object();
private static final CountDownLatch two = new CountDownLatch(1);
private Locks(){}
static final class One implements Runnable {
public void run() {
synchronized(thing) {
two.countDown();
out.println("About to sleep one");
try {Thread.sleep(3600000);}
catch (InterruptedException _) {}
}
}
}
static final class Two implements Runnable {
public void run() {
out.println("Starting two");
synchronized(thing) {
out.println("Two body, got lock");
}
}
}
public static void main(String[] argv) {
Thread onet = new Thread(new One(), "one");
Thread twot = new Thread(new Two(), "two");
onet.start();
try {two.await();}
catch (InterruptedException _) {}
twot.start();
}
}
|