Skip to content

Instantly share code, notes, and snippets.

@sjamesr
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjamesr/aad347c191880ce9001d to your computer and use it in GitHub Desktop.
Save sjamesr/aad347c191880ce9001d to your computer and use it in GitHub Desktop.
class Santa {
volatile int reindeers = 0;
// Call this in any old thread when a reindeer arrives
public synchronized void reindeerArrived() {
reindeers++;
notify();
}
// Returns only when 8 or more reindeer have come in
public synchronized void awaitAllReindeer() throws InterruptedException {
while (reindeers < 8) {
wait();
}
}
}
@Test
public void testChristmas() throws InterruptedException {
final Santa santa = new Santa();
for (int i = 0; i < 8; i++) {
final int waitTime = i;
new Thread(new Runnable() {
@Override
public void run() {
Uninterruptibles.sleepUninterruptibly(waitTime, TimeUnit.SECONDS);
santa.reindeerArrived();
}
}).start();
}
santa.awaitAllReindeer();
System.out.println("Here!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment