So in Java concurrency, there is the concept of a task which is really any implementing Runnable or Callable (and, more specifically, the overridden run() or call() method of that interface).
I’m having a tough time understanding the relationship between:
- A task (
Runnable/Callable); and - An
ExecutorServicethe task is submitted to; and - An underlying, concurrent work queue or list structure used by the
ExecutorService
I believe the relationship is something of the following:
- You, the developer, must select which
ExecutorServiceand work structure best suits the task at hand - You initialize the
ExecutorService(say, as aScheduledThreadPool) with the underlying structure to use (say, anArrayBlockingQueue) (if so, how?!?!) - You submit your task to the
ExecutorServicewhich then uses its threading/pooling strategy to populate the given structure (ABQ or otherwise) with copies of the task - Each spawned/pooled thread now pulls copies of the task off of the work structure and executes it
First off, please correct/clarify any of the above assumptions if I am off-base on any of them!
Second, if the task is simply copied/replicated over and over again inside the underlying work structure (e.g., identical copies in each index of a list), then how do you ever decompose a big problem down into smaller (concurrent) ones? In other words, if the task simply does steps A – Z, and you have an ABQ with 1,000 of those tasks, then won’t each thread just do A – Z as well? How do you say “some threads should work on A – G, while other threads should work on H, and yet other threads should work on I – Z”, etc.?
For this second one I might need a code example to visualize how it all comes together. Thanks in advance.
Your last assumption is not quite right. The
ExecutorServicedoes not pull copies of the task. The program must supply all tasks individually to be performed by theExecutorService. When a task has finished, the next task in the queue is executed.An
ExecutorServiceis an interface for working with a thread pool. You generally have multiple tasks to be executed on the pool, and each operates on a different part of the problem. As the developer, you must specify which parts of the problem each task should work on when creating it, before sending it to theExecutorService. The results of each task (assuming they are working on a common problem) should be added to aBlockingQueueor other concurrent collection, where another thread may use the results or wait for all tasks to finish.Here is an article you may want to read about how to use an
ExecutorService: http://www.vogella.com/articles/JavaConcurrency/article.html#threadpoolsUpdate: A common use of the
ExecutorServiceis to implement the producer/consumer pattern. Here is an example I quickly threw together to get you started–it is intended for demonstration purposes only, as some details and concerns have been omitted for simplicity. The thread pool contains multiple producer threads and one consumer thread. The job being performed is to sum the numbers from 0…N. Each producer thread sums a smaller interval of numbers, and publishes the result to theBlockingQueue. The consumer thread processes each result added to theBlockingQueue.