Producer-Consumer with ExecutorService.newFixedThreadPool - How many threads are created?

Yes. If the consumer Runnable process takes a long time only 20 will run concurrently. The rest will wait in a collection until a thread is available to run it.

As for if there is a better way to do this. Is there a reason you need to use threads? Unless you have 20 available processors running this in parallel may not increase your processing time because all of the threads will be spending time in context switches etc. That are nut useful for processing the data.

Also, the producers are getting all of the data and storing it in the Consumers. If the consumers cannot run because you have 500 of them and only 20 can run at once then you are storing (500 minus 20) * the data you can process. You could have the consumers fetching their own data.

In response to comment: instead of for (int index = 0; index Execute(producer); } and Processor for (Long id: list) { data = get data from db for the id consExec. Execute(new Consumer(data)); } Consumer looks like: public class Consumer implements Runnable { long myId; Consumer(long id){ myId = id; } public void run() { data = get data from db for the id // do whatever a consumer does with data } } and private void start(String args) { // Get list of ids create a new consumer for each id for (int index = 0; index Execute(new Consumer(everyIDi)); } } Then you loose a whole class and the 20 pool makes more sense because Consumers that are blocked on IO fetching data will get waited and ones that are ready can continue processing.

You could have the consumers fetching their own data. " - Do you mean using a queue in between? – Oxford Aug 19 at 17:05.

The pool size is what determines the number of worker threads. If you try to submit an item while all the worker threads are busy, it will be queued by the ExecutorService and run once a worker becomes free. The javadocs say this: Creates a thread pool that reuses a fixed set of threads operating off a shared unbounded queue.

If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. Note the hilighted parts. The number of threads is fixed, and the queue is unbounded, meaning items submitted when the threads are busy will always be queued, rather than rejected.

Thanks. So, in my code above the 500 tasks would be queued and executed by 20 worker threads? If so, does creating 500 Runnables as in the above example have an impact on the application?

Am I not creating too many Runnables? – Oxford Aug 19 at 16:59 That's right, the tasks woudl be queued up and utilize only the 20 threads. Creating 500 Runnables shouldn't be an issue; they're pretty lightweight, and will likely die quickly anyway.

The solution seems fine to me. – dlev Aug 19 at 17:01 Except that the 500 consumers are holding all of the data they are to process. – Clint Aug 19 at 17:08 @Clint Fair point.

The usual pattern is to have the consumers fetch the data off of a producer queue. – dlev Aug 19 at 17:09.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions