Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

A (might-be-basic) question about task dependency in thread pool  RSS feed

 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a question about thread pool (or executor framework).

The requirement is:
1) I have n number of tasks (maybe 1000)
2) Some of those tasks share the same resource. For now, lets say there are total 10 resources, and each resource is shared by 100 tasks. What I mean by this is - the resource will be used(i.e. locked) while the task is being executed.

Hence, to avoid bottleneck, what I'm trying to achieve is - at any given point of time, the thread pool must hold totally independent tasks (e.g. in above situation, there would be only 10 tasks in thread pool).

So - my question is - do we have any ready-made dependency resolution mechanism for it (I can identify which resource a specific task would be using - by calling getter method)? Or do I have to manually maintained 'shuffled queues' (i.e. work queue which contains only independent tasks)?

Any suggestion/approach is welcome.

Thanks.

P.S. I'm just having a gut feeling that somebody must've faced this issue before and there must be not-so-difficult solution for this (in which case I would be happy and embarrassed )
 
Stephan van Hulst
Saloon Keeper
Posts: 6966
109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an interesting problem. I'd almost recommend mapping resources to thread pools, and submit a task to the threadpool associated with that resource. I don't know if there's a ready made solution that's more elegant though.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephen.

So - you mean to say I should create different thread pools per resource (and each thread pool will be single threaded pool) - right?

It sounds good enough

Just trying to figure out if we have any ready-made dependency resolution framework.

In my case, the ratio of number of resources to number of tasks might be pretty skewed - that is, maybe 5 tasks will depend upon 1 resource, or maybe 300 tasks will depend upon 1 resource. So, I was thinking about single pool, and a list of queues - in which, a single queue will contain totally independent tasks.

Thus, we can submit a whole queue to thread pool and wait till all tasks finish.

WDYT?

Thanks.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:I was thinking about single pool, and a list of queues - in which, a single queue will contain totally independent tasks.

Thus, we can submit a whole queue to thread pool and wait till all tasks finish.

Each Queue would have to feed in to a single Thread anyway, if not then you could have two threads in the pool taking from a single queue and you would be in the same situation you are now. You couldn't even have one thread pull from one queue and if it finishes the queue start chewing on another one to help speed up the processing for the same reason - you would have two threads working on the same resource.

I am with Stephen, I would go with a single threaded executor per-resource, so the executor acts more as a task-queue than a thread pool. The tricky part might be how to determine which queue a task goes in, and I guess that depends on the resources. If you could make a type which encapsulates which resource your task is working with (i.e. DatabaseTask, FileTask, WebServiceTask), then you could implement your own Executor and provide overloaded execute() methods which take different types and feeds them into the appropriate Queue. If your resources are the same type, just different instances, then you would need some sort of mapping.

I don't know of any runtime resource dependency/value trackers you would run on production code. Things like that tend to be debugging tools and pretty invasive (slow down the application, break sandboxing). I think your approach should be in design rather than in dependency tracking.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve.

With independent-tasked queues, I might end up another bottleneck on queue and/or some threads in the pool sitting idle.

In really skewed input, I think the independent-tasked queues might help a little, but its cost-to-benefit is not as good as single threaded pool (besides single threaded pool will work in a large variety of situations).

Also, the resource is of same type (and different instances) - so I need to maintain the map. Since number of resource might be more (and more thread pools will have to be created) - so I was trying to avoid this approach, but now I think it is ok (because anyway overhead of creating a single threaded pool does not seem to be much).

Stephen/Steve, thanks for your thoughts!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!