Last active
August 29, 2015 14:10
-
-
Save sjamesr/aad347c191880ce9001d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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