views:

302

answers:

3

Hi...

I had a Java program that I run thousand times based on a loop (according to the number of files to be compiled to build a linux kernel) in a bash script.

There was a performance problem since the jvm was started several times...

What i've done then is implementing a wrapper in java that does the same as my bash script, reads one line from a file and then calls the main of my previous program... This way, I only have one jvm running...

The problem now is that only one core of my box is used which is another performance issue... Do I have to start some threads or can I use the same method but maybe calling the "former" main in a different way ? If i have to start some threads, how I dispatch them throughout the multiple cores ?

thanks...

+15  A: 

Your java program needs to become multi-threaded, in order to take advantage of many cores.

For example, create a thread pool using java.util.concurrent.Executors, encapsulate your data items as a Runnable, and submit the Runnable to the threadpool.

skaffman
thanks for the answer... I'll try to do that :)
LB
+1  A: 

You will need to make your program multi-threaded. You will have to do some learning to do this and I recommend you start with the Java Concurrency Tutorial.

Mr. Will
i've already read that...I wanted to be sure...
LB
+3  A: 

At the risk of oversimplifying it, just have your old class implement Runnable, taking what was in main() and putting it in Run(), then create a new thread with that class and start the thread.

In reality it might be more complicated than that if the threads need to share data, but based on what you said you are doing here, it doesn't seem like they would. So it might actually be just that easy.

Eric Petroelje
I've thought about that...but i don't know if it's oversimplified or not...that may be a good start...just to see if it works :-)
LB
You should also do what skaffman said and use Executors and a ThreadPool (if you have Java 5 or later). You do not want 1000 threads running either.
Kathy Van Stone
I agree with Kathy - skaffman's answer should be what you're looking for.
Jason Coco