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
|
public static void main(String[] args) {
final Object lock1 = new Object();
Thread t1 = new Thread() {
{
setDaemon(true);
}
public void run() {
while (true) {
synchronized (lock1) {
try {
lock1.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("t1 has woken up");
}
}
};
t1.start();
Thread t2 = new Thread() {
{
setDaemon(true);
}
public void run() {
while (true) {
System.out.println("t2 will notify");
synchronized (lock1) {
lock1.notifyAll();
}
try {
Thread.sleep((int) (Math.random() * 1000) + 10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t2.start();
try {
Thread.sleep(25000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} |
Partager