ExecutorService

Easy thread pools

I'm making a big assumption here, but most of the commercial world is still stuck on using Java 1.4.x and below. On my new project, I'm in the nice position of being able to use Java 5.0 and all of the new features that it brings to the table. One of these is the ExecutorService, which is a part of the java.util.concurrent package. I wanted to build a thread pool today and this makes it a trivial exercise. Create an ExecutorService instance via one of the factory methods on the Executors class and you're done. When you need to do something with the pool, just throw it a Runnable.

There's tons of stuff in this package that I've not looked at in any detail before. Time to dust off my copy of Java 5.0 Tiger: A Developer's Notebook that I bought at JavaOne 2004. :-o

Tags :


Re: ExecutorService

There also is a backport for java 1.4 http://dcl.mathcs.emory.edu/util/backport-util-concurrent/ And I also use the Executor in a lot of projects. It is perfect for executing tasks on. It is a lot better than starting threads yourself because: -you have control on the number of concurrent threads (you don't want uncontroller thread creation) -you have control on the number of unexecuted tasks if you make the taskqueue bounded. And there are other advantages (it makes components also easier to test because you can inject a Mock implementation of an Executor instead of a ThreadPoolExecutor). So the new concurrency library in Java 5 (and in 1.4 with the backport) is great :)

Re: ExecutorService

Agreed - it's just so nice to have functionality like this out of the box. :-)

Add a comment Send a TrackBack