Skip to content

Instantly share code, notes, and snippets.

@jawher
Created February 23, 2012 16:30
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jawher/1893580 to your computer and use it in GitHub Desktop.
Solution to the puzzles dream : http://wouter.coekaerts.be/2012/puzzle-dreams
class Dream {
public void dream(final Sleeper s) {
new Thread(new Runnable() {
@Override
public void run() {
s.enter(new Dream() {
public void dream(Sleeper s) {
synchronized (s) {
s.notify();
}
synchronized (s) {
try {
s.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
}).start();
synchronized (s) {
try {
s.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
@coyotesqrl
Copy link

Nice solution. My only suggestion would be to get a handle to that thread and set it as a daemon so the JVM can exit.

I say this as someone who spent a ridiculous number of hours banging my head against a wall yesterday and finally gave up, knowing full well when I saw a solution I'd see how close I'd gotten and be frustrated with myself.

@jawher
Copy link
Author

jawher commented Feb 25, 2012

thanks !

You are indeed right, the program will run indefinitely, but since the puzzle objective was attained, didn't bother with cleaning up afterwards :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment