views:

140

answers:

4

I'm using the native ProcessBuilder class to create a variably sized list of process objects. Currently, I simply roll through the list calling the start method on each Process instance. I would however like to better control this aspect of the application by limiting the number of my running processes to a custom value (e.g. number of processors). What data structure(s), existing library, or model best suit this task?

Example

List<Process> processList = new ArrayList<Process>();
ProcessBuilder pb1 = new ProcessBuilder("myCommand", "myArg1", "myArg2");
ProcessBuilder pb2 = new ProcessBuilder("myCommand", "myArg1", "myArg2");
processList.add(pb1);
processList.add(pb2)

for(Process p : processList)
(
  // more control is needed here
  p.start();
}
A: 

IIRC there is a generic Pool in Apache commons that might be well suited.

Hemal Pandya
A: 

You could implement Runnables that run the command synchronously, and then use the ThreadPoolExecutor to control the threads.

fabstab
+1  A: 

You could use a Blocking Queue(a collection whose size you can bound and will block on storage if there's no space) to store running processes, deleting processes from the queue when they're no longer in use. Keep the waiting processes in a pool or a queue and inject them into the (running) queue with a separate thread as space becomes available.

Steve B.
+1  A: 

Use the ThreadPoolExecutor. A simple example:

public class PBTest {

static List<Runnable> list = new ArrayList<Runnable>();

public static void main(String args[]) {
    ProcessBuilder pb1 = new ProcessBuilder("foo.exe");
    ProcessBuilder pb2 = new ProcessBuilder("foo.exe");
    ProcessBuilder pb3 = new ProcessBuilder("foo.exe");
    addPB(pb1);
    addPB(pb2);
    addPB(pb3);

    ExecutorService ex = Executors.newFixedThreadPool(1);
    for (Runnable r : list) {
        System.out.println("calling execute");
        ex.execute(r);
    }

    ex.shutdown();
}

static void addPB(final ProcessBuilder pb) {
    list.add(new Runnable() {
        public void run() {
            try {
                pb.start();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    });
}

}

JZeeb
I see fabstab answered the question while I was composing my reply.
JZeeb