Friday Java Quiz: What's The Exit Status
Most of you probably will read this in Saturday morning. However it's still Friday here, so I'll send this out as a Friday Java Quiz.
The Thread.UncaughtExceptionHandler interface in Java 5 allows Java programs to react to Throwables that are not caught and handled by any method on a thread's call stack.
The following class registers a global uncaught exception handler that is invoked when a Throwable bubbles up to the root of any thread stack:
1 public class Main { 2 public static void main(String[] args) { 3 Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { 4 public void run() { 5 // Clean up resources: close open files, close databases, etc. 6 } 7 })); 8 Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { 9 public void uncaughtException(Thread t, Throwable e) { 10 if (e instanceof Error) { 11 System.exit(99); 12 } 13 } 14 }); 15 // The application proper: starting the GUI, listening on ServerSockets, etc. 16 } 17 } 18
Assume a memory leak in the application causes the JVM to run out of memory and the JVM starts to throw OutOfMemoryError that eventually causes line 11 to be executed and the process is terminated soon afterwards.
Q: What will the exit status of the Java process as seen by the shell be? Is it 99? Can it be anything other than 99?
This is a tricky question. The relaxed rule apply: you can use Google, read books or even compile and run test codes.
That Was Blog Post 1024
I just noticed that the last post I made this morning was blog post number 1024. I have been planning to say something special on my 1024th blog since blog post 512. Something silly along the lines of 100000000002 posts in 2000000000010 milliseconds. Now I'll have to wait for blog post number 2048.
Just to show you that I have not lost touch with the real world, I'll offer my recent observation, about twitter. I just saw it yesterday in one of the twits that was retwitted by someone I follow. It is in Chinese and referred a woman who twit as 女推. I think the English translation should be twittress or twitress.
James Carr: Google Collections At The JUG
Oh boy. It's been a month since my last post. Is this what happens to all the other bloggers who went quasi-silent? I don't think I have a good excuse. I've not been "busy," at least not more than in the past. I've not been seeing less amusing(ly stupid) Java code than before. The only one I can think of is that somehow I think the things I might want to say is not interesting to anybody anymore. I guess fifteen years is long enough for all the interesting things to be said about Java.
But that's not true. Yesterday at the St. Louis JUG, James Carr gave a pretty interesting presentation about Google Collections. I'll point you to his slides and let you get a feel of the atmosphere at the talk:
I'll just mention a few things, my reactions as I was listening:
- This things has been out for a long time, right? Why haven't I used it in my projects?
- Is this how you write JUnit assertions now: assertThat(3, equals(3))?
- Joiner is cool indeed!
- These are all good things: no public constructors; static factories; MultiSet; BiMap; MultiMap
- This thing really is easy to explain and learn.
- The "Builder" pattern is nice. But is it really *The* GoF Builder Pattern?
- What's that presentation software James is using. It's so cool.
Thanks James.