0

I would like to run a jar file saved on my local disk multiple times. The jar file should run many times in an given instance. All the jar file(Same file) should run together. Is there a way i can Achieve it? Do i have to write a code, that would create multiple thread?

Now for the time being i open up many command prompt and call the jar in the cmd separetly, hence simultaneously running them. But this is not an effciient way. Is there an more efficient way to do this?

Any help would be very much appreciated.

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
Spaniard89
  • 2,359
  • 4
  • 34
  • 55
  • 2
    Define "together", "simultaneously", "efficient". –  Sep 07 '12 at 08:08
  • @Tichodroma Simultaneously: When all the jar file are running in the same instance. Together: it is analogous to simultaneously in my post above. Efficient: I cannot open up 20-30 cmd window and run them separately, its heavy load onto processor. – Spaniard89 Sep 07 '12 at 08:19
  • How many CPUs do you have? If you have 8 cores and each process using 1-2 CPUs, you may find that running 4-8 is the best solution. – Peter Lawrey Sep 07 '12 at 08:24

2 Answers2

4

I cannot open up 20-30 cmd window and run them separately, its heavy load onto processor

You have the overhead of (20-30)-1 JVMs with that method. You can create a second main class like this:

public static void main(final String[] args) {
    int numberOfSimultaneousExecutions = 25;
    java.util.concurrent.Executor executor = java.util.concurrent.Executors.newFixedThreadPool(numberOfSimultaneousExecutions);
    for (int i = 0; i < numberOfSimultaneousExecutions; i++) {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                RealMainClass.main(args);
            }
        });
    }
}

Now the JAR is running simultaneously in one JVM.

However, this is dangerous. If your code is accessing (and modifying) any shared static values your application will almost certainly break.

You can also take the number of simultaneous executions from the arguments:

public static void main(final String[] argsWithNum) {
    int numberOfSimultaneousExecutions = Integer.parseInt(argsWithNum[0]);
    final String[] args = new String[argsWithNum.length - 1];
    System.arraycopy(argsWithNum, 1, args, 0, args.length);
    java.util.concurrent.Executor executor = java.util.concurrent.Executors.newFixedThreadPool(numberOfSimultaneousExecutions);
    ...
main--
  • 3,873
  • 1
  • 16
  • 37
  • Do i have to embedded this piece of code onto the main class of mine? and if I do that, how can a class accumulate two main? – Spaniard89 Sep 07 '12 at 08:38
  • 1
    @Kishorepandey No, you should create a **second** main class with that main method and replace `RealMainClass` in the code with the name of your normal main class. – main-- Sep 07 '12 at 08:43
  • I am sorry mate, I got it now. RealMainclass is the name of the class that i want to run many time! – Spaniard89 Sep 07 '12 at 08:48
  • what if my realmainclass has some arguments that would want to pass? as i would be referring this main in my jar. How would pass the arg in my realmainclass from this main? Thanks a ton for replying mate! – Spaniard89 Sep 07 '12 at 08:50
  • As you can see, the code calls `RealMainClass.main(args)` so the arguments you pass to this `main()` are handed on to the real `main()`. – main-- Sep 07 '12 at 08:55
  • Thanks for helping me out mate! It was really useful. I just have one last question, What if i also want to pass numberOfSimultaneousExecutions as an argument? what should i do? – Spaniard89 Sep 07 '12 at 09:05
  • 2
    I added an example for that in my answer – main-- Sep 07 '12 at 09:11
0

To run something you need a thread. If you want to run something multiple times concurrently, you need multiple threads. Not all code is written to be thread safe, so you need to check the code will behave correctly if you do this.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • How would check that and how would i accomplish creating multiple thread? – Spaniard89 Sep 07 '12 at 08:22
  • I would submit tasks to an ExecutorService (google for more details). The only way to be sure that code is thread safe is to read the documentation and if that doesn't say, read the code to see how it works. It may be that running multiple processes is the simplest solution. – Peter Lawrey Sep 07 '12 at 08:23