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) {}
                }
            };
    }

Comments:

:-)

Posted by guest on July 24, 2010 at 02:37 AM PDT #

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.>>

Posted by Kostis Anagnostopoulos on July 26, 2010 at 01:40 AM PDT #

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

Posted by random programmer on July 26, 2010 at 05:44 AM PDT #

@Kostis Anagnostopoulos stop spreading.

Posted by dreamscape on July 27, 2010 at 12:13 AM PDT #

very nice \^_\^

Posted by mark on August 19, 2010 at 03:17 PM PDT #

Post a Comment:
Comments are closed for this entry.
About

darcy

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
News

No bookmarks in folder

Blogroll