1

I have a loop which iterate for 10 times and in each time it executes some interrelated methods.So is there any solution that can do the same task paralally for each instance of that loop.

1st core should execute the program for i=0,1,2,3
2nd core should execute the program for i=4,5
3rd core should execute the program for i=6,7
4th core should execute the program for i=8,9

Assume I have 4 cores

devshorts
  • 8,572
  • 4
  • 50
  • 73
Bishnu
  • 383
  • 4
  • 14

2 Answers2

1

I have a loop which iterate for 10 times and in each time it executes some interrelated methods.So is there any solution that can do the same task parallaly for each instance of that loop.

First thing to realize is that you don't have specific control over task to core mapping unless you utilize 3rd party libraries. In general you don't want to do this anyway -- just let the JVM be smart about it.

To utilize all of the cores on your hardware, typically you change the number of threads in a fixed thread pool. You can use a cached thread pool which will increase the tasks depending on the number of tasks submitted to the ExecutorService. On modern JVMs and operating systems thrashing is less of a worry so this works well unless you are talking about 1000s of tasks.

But typically I set a fixed number of threads in the pool. If the tasks are 100% CPU bound then I may set the number of threads to match the number of virtual processors the JVM has. Typically with log output, network transactions, and other IO you might need to increase the number of threads to utilize all of the processors. I'd run your program a couple of times with different values.

In your case you seem to want to specifically run jobs 0,1,2,3 in one thread while 4,5 run in another thread in parallel (hopefully). To accomplish this you should submit 4 tasks to your ExecutorService and have each task run the iteration numbers that you want.

Maybe something like:

 executorService.submit(new MyTask(new int[] { 0, 1, 2, 3}));
 executorService.submit(new MyTask(new int[] { 4, 5 }));

If the number to thread mapping is arbitrary then I'd just submit 10 tasks to the ExecutorService and it will run the tasks in parallel in a FIFO manner.

Gray
  • 115,027
  • 24
  • 293
  • 354
0

Look into Executors and ExecutorService. You can create a thread pool and throw any number of Runnables at it. For instance, if you want to have one thread per core, you can do something like:

ExecutorService pool = 
    Executors.newFixedThreadPool(Runtime.getRuntime().getAvailableProcessors());

Then you can just do this:

pool.execute(myRunnable);

There is a very good example in the API docs: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html

jgitter
  • 3,396
  • 1
  • 19
  • 26
  • If my system has 4 cores and i suppy 10 in place of Runtime.getRuntime().getAvailableProcessors() then what will happen...? – Bishnu Feb 24 '14 at 21:11
  • Thank u for your answer....it is useful..!!Keep helping sir. – Bishnu Feb 24 '14 at 21:11
  • You can have more threads running than you have cores, in fact it is uncommon that you don't. If you have very CPU intensive threads, it is actually important that you limit the number of threads in the pool so you don't overwhelm the computer. – jgitter Feb 24 '14 at 21:32