Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi:Threading - Is this the right approach?

Experts -

I need some advice in the following scenario.

I have a configuration file with a list of tasks. Each task can have zero, one or more dependencies. I wanted to execute these tasks in parallel [right now they are being executed sequentially]

The idea is to have a main program to read the configuration file and load all the tasks. Read individual tasks and give it to an executor [callable] that will perform the task and return results in a Future. When the task is submitted to the executor (thread) it will monitor for its dependencies to finish first and perform its own task.

Is this the right approach? Are there any other better approaches using java 1.5 features?

like image 873
jagamot Avatar asked Mar 26 '10 15:03

jagamot


1 Answers

Sounds fine, but beware of Thread starvation deadlock. Basically, don't use a bounded thread pool.

Here is a example that illustrates this problem.
http://www.javaconcurrencyinpractice.com/listings/ThreadDeadlock.java

Also, if you have e.g. a pooled DB connection, you might run into problem, too. 10 threads can block, holding all the pooled connection, waiting for the 11th thread that can't acquire the 11th pooled connection because there isn't anymore available..

like image 118
Enno Shioji Avatar answered Oct 10 '22 23:10

Enno Shioji