• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dispatcher where some object is always processed by the same thread?  RSS feed

 
Robert Nicholson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you have data to process and you want to dispatch each item based on id using modulo the number of threads you have to process any given item such that all items with the same id are processed by the same thread. Presumably this requires an addressable set of ExecutorServices?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I would probably create an ExecutorService[] array, and use Executors.newSingleThreadExecutor() to populate each element. Then for each thing to process, pick the appropriate array element, and submit a task to that ExecutorService.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is a SingleThreadExecutor significantly better than just a BlockingQueue with a consumer on other the end?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really - that's basically what newSingleThreadExecutor() gives you. It's just convenient to be able to do that in one method call. And using the ExecutorService interface can make it easier to refactor your code to use other threading implementations later, if you want to. Both benefits are relatively minor here, I think. But nowadays I like to look to the Executors class first for solutions when doing threading.
[ December 19, 2007: Message edited by: Jim Yingst ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kool, thanks. Eggzellent point about the flexibility to choose other executors.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure why you have insisted on using the same thread?
While using an executor, thread instance is not what a client must be bothered about, instead it should be worried about the runnable/callable that is submitted.

I would presume that the task(runnable/callable) that you will submit will have the id of the item that is to be processed and enough context information to process that item.
When the task gets invoked, it can look at the id and dispatch it to the appropriate class to execute it. This will all happen in the thread of the executor.
With this you can change the executor anytime you want and all the same task ids will be processed by a dedicated class concurrently.
Additionally, you can assign a priority to each task which you will not be able to do if you have different executors for every task id.

P.S. I think this is what Stan was referring to. isn't?
Hope it answers your question.
[ December 20, 2007: Message edited by: Nitesh Kant ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!