0

I'm calling two different main class which are within a for in my code, However I would like to run both of them at the same time.

for (int m=0; m<ListUser.size();m++){
System.out.println(ListUser.get(m));
 File user = new File(ManagedPlatformPath.properties()+"/"+ListPlatform.get(n)+" /"+ListUser.get(m)+".adf");
  if(user.exists()){
   System.out.println("Reading Information "+ListUser.get(m)+"");
    BACControlS.main(args);
    BACControlT.main(args);
  }
  else{
    System.out.println("Not Information "+ListUser.get(m)+"");
  }

How would be possible to run both BACControlS.main(args) and BACControlT.main(args) at the same time, instead to wait until one is finish.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sallyerik
  • 489
  • 2
  • 11
  • 24

4 Answers4

1

Spawn two threads.

new Thread(new Runnable() {
    public void run() {
        BACControlS.main(args);
    }
}).start();

new Thread(new Runnable() {
    public void run() {
        BACControlT.main(args);
    }
}).start();

In order to pass args down to those Runnable you may need to declare args as final

Raffaele Rossi
  • 1,079
  • 5
  • 11
  • 1
    That's probably not that good an idea in a loop, if 'ListUser' had 1M users, you'd attempt to spawn 1M threads! Look at using a `java.util.concurrent.Executor` to provide a pool of threads and pass the `java.lang.Runnable`s to it. – Nick Holt Jan 17 '14 at 11:29
1

You should use threads for that. You can run it as:

Thread t1 = new Thread(new Runnable() {

@Override
public void run() {
  BACControlS.main(args);       
            }
});
t1.start();
Thread t2 = new Thread(new Runnable() {

@Override
public void run() {
  BACControlT.main(args);       
        }
});
t2.start();

Instead of

BACControlS.main(args);
BACControlT.main(args);
G.S
  • 10,413
  • 7
  • 36
  • 52
0

There are no means available to run 2 classes as both main classes are suppose to be different processes. So what you can do is use threads. Threads can run parallel. If both treads are in one process, CPU takes it as a single process and treat it as a single process.

0

Use an java.util.concurrent.ExecutorService. Creating java.lang.Threads in a loop, as other anwsers suggest, is a bad idea because threads are not limitless resources but your list of user names could be large.

Here's how the code would look with a java.util.concurrent.ExecutorService:

ExecutorService executorService = Executors.newFixedThreadPool(10);

try
{
  for (String userName : users)
  {
    File userFile = ...

    if (userFile.exists())
    {
      System.out.println("Reading Information " + userName);

      executorService.execute(
        new Runnable()
        {
          public void run()
          {        
            BACControlS.main(args);
          }
        }
      );

      executorService.execute(
        new Runnable()
        {
          public void run()
          {        
            BACControlT.main(args);
          }
        }
      );
    }
    else
    {
      System.out.println("Not Information " + userName);
    }
  }
}
finally
{
  executorService.shutdown();
}
Nick Holt
  • 33,455
  • 4
  • 52
  • 58
  • Two questions: It seems work but for any reason the process never finish. I mean I'm running my class in Netbeans and the process is running till I decided to stop it. – Sallyerik Jan 17 '14 at 12:44
  • How would be possible wait till the two threads are finish and then exit the loop? – Sallyerik Jan 17 '14 at 12:45
  • The threads in the `executor` are keeping the JVM alive, so you need to call `ExecutorService.shutdown()` after the loop - I'll modify the answer to demostrate. – Nick Holt Jan 17 '14 at 13:04
  • Great works fine!!! I've done some changes to wait till all threads are finished and then exit the loop. http://tutorials.jenkov.com/java-util-concurrent/executorservice.html – Sallyerik Jan 17 '14 at 14:09
  • The code will work as it is, `ExecutorService.shutdown` waits for all tasks to be completed before shutting down, see - http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html#shutdown() – Nick Holt Jan 17 '14 at 15:41
  • Yes, you're totally right, but what I wanted to do is to keep my proceess in the loop till all the task within the loop are finished and then exist the loop a run some other task, which has to be run once the loop task are finished. – Sallyerik Jan 20 '14 at 08:57