Eric Burke: Java Concurrency By Example
We had an outstanding presentation at the St. Louis JUG yesterday evening. Eric Burke, of the It's Just a Bunch of Stuff That Happens fame, gave a presentation on some of the new Java 5 and 6 concurrency features, through a series of examples. Like in the past, this is another one of those "you have to be there to experience it" presentations.
Here's what I learned from this presentation:
- The Java Concurrency in Practice book, by Brian Goetz, with Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, and Doug Lea, is a must read for anyone developing Java applications on a modern Java platform.
- Have you written something like this
try { Thread.sleep(1000); } catch (InterruptedException e) { // ignored }This is completely wrong. - i++ is not atomic.
- The new concurrency utilities package in Java 5 and 6 is very rich. It's worth studying in depth.
- TimeUnit
- CopyOnWriteArraySet
- Use a ThreadFactory to create threads and give them proper names instead of the default "Thread-1", "Thread-2" and register UncaughtExceptionHandler's.
- Executor, Future
- Lock, tryLock(), lockInterruptibly()
- ReadWriteLock
- BlockingQueue
- Here's how a thread should be written
new Thread() { public void run() { while (!Thread.currentThread().isInterrupted()) { // do the work... } } }
At the meta level, I think the lesson is there are so much stuff that's new in Java 5, that it's worth learning it anew.
On a different topic in the post presentation chats, Eric mentioned that the latest Java 6 have many improvements over Java 5. And if you are upgrading from Java 1.4, you might as well skip Java 5 and jump directly to Java 6