X

Musings on JDK development

Understanding Inception using try-with-resources

After viewing the film "Inception," I was inspired to use the new try-with-resources statement to summarize the film:

    public static void main(String... args) {
try(CloseableDreamLevel dl1 =
kickOnClose(createCityScape(firstClassCabin)) ) {
try(CloseableDreamLevel dl2 =
kickOnClose(createHotel(dl1))) {
try(CloseableDreamLevel dl3 =
kickOnClose(createSnowMountainFortress(dl2))) {
inception();
try(CloseableDreamLevel dl4 =
kickOnClose(enterLimbo(dl3))) {
findTheLost();
}
}
}
}
}
private static DreamLevel enterLimbo(DreamLevel dl) {
return
new DreamLevel() {
void kick() {
try {
if ((new java.util.Random()).nextBoolean())
Thread.currentThread().sleep(Integer.MAX_VALUE);
} catch (InterruptedException ie) {}
}
};
}

Join the discussion

Comments ( 5 )
  • guest Saturday, July 24, 2010

    :-)


  • Kostis Anagnostopoulos Monday, July 26, 2010

    Please help stop spreading bad programming habits.

    The problem with the code above is the empty catch block of InterruptedException,

    that is, not re-asserting the interuption.

    Taken directly from http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html:

    (see "How do I stop a thread that waits for long periods (e.g., for input)?")

    <<... any method that catches

    an interrupt exception and is not prepared to deal with it immediately

    reasserts the exception. We say reasserts rather than rethrows, because it is not always possible to rethrow the exception.

    If the method that catches the InterruptedException is not

    declared to throw this (checked) exception, then it should "reinterrupt

    itself" with the following incantation:

    Thread.currentThread().interrupt();

    This ensures that the Thread will reraise the InterruptedException as soon as it is able.>>


  • random programmer Monday, July 26, 2010

    a very BROAD overview but clever nonetheless

    :)

    and to Kostis Anagnostopoulos...do you really think this post was meant to be technical at all??

    maybe you need some sleep...or a vacation because you take things TOO seriously


  • dreamscape Tuesday, July 27, 2010

    @Kostis Anagnostopoulos stop spreading.


  • mark Thursday, August 19, 2010

    very nice \^_\^


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.