Java 5 introduces support for thread pools

I’ve been having a bit of a play with Java 5 to see what is new, and I’m impressed. There’s lots of neat little things to give developers less to worry about. One of these is the introduction of in-built support for thread pooling.


Java’s support for (albeit inefficient!) multi-threaded code is one of the things that I’ve liked about it for a while. In simple apps that I’ve hacked together before, I’ve done it inline like this:

// do something here 

new Thread(new Runnable() {
    public void run() {
        // do somethere here that takes a long time
        // and we dont want to have to wait more
    }
}).start();

// carry on doing something else here

This is fine for simple apps that don’t do much, but if you do it for short bits of code that get called very often, you end up with the overhead of creating and garbage-collecting threads all the time and performance takes a nosedive. For bigger Java projects I’ve done – like Healthcheck – I’ve had to include a pool of worker threads. Instead of creating a new thread every time I need to do something in the background, I set up a bunch of threads at the beginning to do it. When they finish, they can either move on to the next job, or wait until something needs doing.

Doing this properly – handling all the possible problems, allowing background jobs to be cancelled, and so on – is a bit fiddly, and essentially a distraction from whatever it is you’re trying to write.

So I was really pleased to come across some of the new java.util.concurrent classes. In particular, the support for thread pools. You can ask Java to create you a thread pool – and either leave all the settings on their defaults, or configure it to your needs, such as specifying the number of threads it should maintain. You can even let it manage this dynamically and create new threads if it gets busy. Then, all you have to do is wrap your background job code in Runnable and pass it to the thread pool to be added to the queue of jobs.

For example, for the code I was writing, I used this:

// created as a static object for my whole app
ExecutorService executor = Executors.newCachedThreadPool();

Then wherever I had a task that needed to be run outside the main thread, I did something like this:

Runnable myNewTask = new Runnable() {
    public void run(){
        // my background work
    }
};
		
executor.execute(myNewTask);

There is a lot more I can do to configure executor, as well as probably improve error-handling, but as a start this is a great improvement on Java 1.4. Even for the bit of code that I was playing with today, the performance difference was obvious.

Anything like this that frees a developer from having to bother reinventing the wheel for low-level stuff like this every time they write an application, and instead focus on what new things they can provide, is great. We don’t get a chance to play with Java 5 much at work, since we bundle IBM’s Java 1.4.2 with WMQ, but I want to try and see what else is new when I get a chance.

2 Responses to “Java 5 introduces support for thread pools”

  1. Aditi says:

    I was just looking at implementing Thread Pools. The current implementation is using Jboss thread pooling. Was wondering if shifting to the inbuilt java thread pool handling improve the performance? Would you have any idea about it?