Link to home
Start Free TrialLog in
Avatar of ku916
ku916

asked on

Multi threading with queue

Hi, all

We have this web application that is written in Servlet and Java Beans on NT running Tomcat that would execute command line programs in the background in a sequential manner. Now I want to upgrade this process so that it would run multiple programs (app setting on the number concurrent jobs, maybe 5).

So when a user select 6 programs to run from the app, the app would process 5 of the 6 programs and as soon as 1 of them finish it would run the 6th program.

I've look into multi-threading and it seems not too difficult to implement, it could look something like;

public class SimpleThread extends Thread {
public SimpleThread(String str) {
super(str);
}
public void run() {
<Run Program here!>
}
}

But how can I implement the queue (may not fit in the definition of a queue) like function to run 5 jobs at a time with the 6th waiting?
Avatar of Venabili
Venabili
Flag of Bulgaria image

Why don't you try the following:

Create a List(ArrayList, Vector - any ordered sequence) with all the jobs that are waiting to be started and a List with the started ones. On deletion of the started one, start the first from the not started and move it to the other list. I hope you have a good way to determine if a program is stopped.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cavey79
cavey79

Indeed go for thread pool as cehj suggested. It's the simplest and most elegant solution. Might be reusable too ;)
ku916,

Can you please finalize that thread - awarding points to the expert(s) that helped you or posting a request for that thread to be deleted here:
https://www.experts-exchange.com/Community_Support/

Thanks
Venabili