0

As far I know, thread pools (java.util.concurrent.Executor class) provide a queue of tasks for all threads in a pool. So I don't really know, which thread will execute my task. But I need to have queues of tasks assigned to every thread. How can I do it?

koral
  • 2,513
  • 1
  • 32
  • 41
notnavol
  • 3
  • 1
  • 3
    Why do you care which thread executes one particular task as long as the job gets done eventually? – fge Jun 02 '13 at 19:50

2 Answers2

1

If you want only certain threads to execute certain tasks you a standard Threadpool will not fit. But you can use multiple Threadpools with only one thread in each to solve your problem.

mschenk74
  • 3,561
  • 1
  • 21
  • 34
1

You should write your program so you don't need to know what thread executes a task. They are just anonymous worker threads.

However, if you really want to know anyway you can create a single threaded ExecutorService for each thread you want and then you will know which thread will execute a task.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • You right, but in my program all input separated between workers in special order, that's why when I get new input I decide which worker will execute it, and then I put this input into his thread. I see, that it's really not a good idea. I'll try to avoid this trap. Thanks for your answer. – notnavol Jun 03 '13 at 05:36
  • @notnavol I would be careful about splitting input in this way. If you cut it up too finely you can end up with a solution where the overhead is 10x the work you are performing (or even much more) This can make a multi-threaded solutions much, much slower than a single thread solution. – Peter Lawrey Jun 03 '13 at 06:51