Solution to the puzzles dream : http://wouter.coekaerts.be/2012/puzzle-dreams
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 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(); | |
} | |
} | |
} | |
} |
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
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.