The following is a demo of the equivalent of using wait and notify in Java 4 using the java.util.concurrent package introduced in Java 5. What it does is explained in the code comments. It's instructive to compare the two ways of doing things, and you can see the wait/notify package version HERE. The source for the code below is HERE.
import java.util.concurrent.locks.Condition;
/**
* Show how wait and notify can be implemented
* in the java.util.concurrent packages.
*
* We create a Thread 'sleepy' that keeps
* falling asleep and will be awoken at
* random intervals by several 'waker' threads
*
* @author Charles Johnson
*/
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class WakeUpJ5 {
final Lock lock = new ReentrantLock();
final Condition waker = lock.newCondition();
public void wake() throws InterruptedException {
try {
lock.lock();
waker.signal();
} finally {
lock.unlock();
}
}
private void startWakers() {
final int NUM_WAKERS = 5;
for (int i = 0; i < NUM_WAKERS; i++) {
Waker w = new Waker();
w.start();
}
}
private void start() {
Thread sleepy = new Thread() {
public void run() {
while (true) {
try {
lock.lock();
waker.await();
System.out.println("'sleepy' says: I'm awake!");
} catch (InterruptedException e) {
// ignore
} finally {
lock.unlock();
}
}
}
};
sleepy.start();
startWakers();
}
public static void main(String[] args) {
WakeUpJ5 w = new WakeUpJ5();
w.start();
}
private class Waker extends Thread {
private long howLong;
private String name;
public Waker() {
// Give it a random interval
howLong = (int) (Math.random() * 5000);
// Give it a name using its interval
name = String.format("%d-ms-waker", howLong);
}
/**
* Sleep for random interval 'howLong' and then wake 'sleepy'
*/
public void run() {
try {
Thread.sleep(howLong);
// Wake up 'sleepy'
System.out.printf("Thread %s about to wake up the sleeper...\n",
name);
wake();
} catch (InterruptedException e) {
// ignore
}
}
}
}
/**
* Show how wait and notify can be implemented
* in the java.util.concurrent packages.
*
* We create a Thread 'sleepy' that keeps
* falling asleep and will be awoken at
* random intervals by several 'waker' threads
*
* @author Charles Johnson
*/
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class WakeUpJ5 {
final Lock lock = new ReentrantLock();
final Condition waker = lock.newCondition();
public void wake() throws InterruptedException {
try {
lock.lock();
waker.signal();
} finally {
lock.unlock();
}
}
private void startWakers() {
final int NUM_WAKERS = 5;
for (int i = 0; i < NUM_WAKERS; i++) {
Waker w = new Waker();
w.start();
}
}
private void start() {
Thread sleepy = new Thread() {
public void run() {
while (true) {
try {
lock.lock();
waker.await();
System.out.println("'sleepy' says: I'm awake!");
} catch (InterruptedException e) {
// ignore
} finally {
lock.unlock();
}
}
}
};
sleepy.start();
startWakers();
}
public static void main(String[] args) {
WakeUpJ5 w = new WakeUpJ5();
w.start();
}
private class Waker extends Thread {
private long howLong;
private String name;
public Waker() {
// Give it a random interval
howLong = (int) (Math.random() * 5000);
// Give it a name using its interval
name = String.format("%d-ms-waker", howLong);
}
/**
* Sleep for random interval 'howLong' and then wake 'sleepy'
*/
public void run() {
try {
Thread.sleep(howLong);
// Wake up 'sleepy'
System.out.printf("Thread %s about to wake up the sleeper...\n",
name);
wake();
} catch (InterruptedException e) {
// ignore
}
}
}
}