views:

152

answers:

4

How do I implement a ThreadPoolExecutor? What needs to be done to use it?

+7  A: 

The API Specification for the ThreadPoolExecutor class has four constructors which can be used to create an instance of the ThreadPoolExecutor.

The Executors class also has methods such as newCachedThreadPool and newFixedThreadPool methods, but those are only listed as returning an ExecutorService, so it doesn't necessarily have to be a ThreadPoolExecutor.

For more general information on Executors and the Java concurrency package, the Lesson: Concurrency from The Java Tutorials has more information. In particular, the Executors and Thread Pools section may be of interest.

coobird
+2  A: 

Executor is a interface. Implementing it allows clients to pass you runnable tasks, which are then executed in a particular manner. ThreadPoolExecutor is one class implementing that interface (and ExecutorService). It uses a thread pool so you can have multiple threads executing jobs without needing a new thread for every job. ThreadPoolExecutor can be subclassed, or you can just instantiate it with one of the public constructors.

Other methods, such as Executors.newFixedThreadPool, return a thread pool of some kind though not (necessarily) a ThreadPoolExecutor.

Matthew Flaschen
+1  A: 

Java Concurrency in Practice - Addison Wesley ISBN:0321349601 I used this book to learn about Executors. I has a bunch of good examples you can steal.

Javamann
A: 

I suggest you start with the tutorial for this class http://java.sun.com/docs/books/tutorial/essential/concurrency/exinter.html

Peter Lawrey