简体   繁体   English

如何在几个内核上执行Java程序?

[英]how to execute a java program on several cores?

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. 我有一个Java程序,该程序基于bash脚本中的循环(根据要编译以构建linux内核的文件数)而运行数千次。

There was a performance problem since the jvm was started several times... 由于jvm启动了几次,所以出现了性能问题。

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... 然后,我要做的是在Java中实现一个包装程序,该包装程序与bash脚本相同,从文件读取一行,然后调用我以前的程序的主体...这样,我只运行了一个jvm。 。

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 ? 现在的问题是,仅使用我的机器的一个核心,这是另一个性能问题...我是否必须启动一些线程,还是可以使用相同的方法,但是可能以不同的方式调用“ former”主线程? If i have to start some threads, how I dispatch them throughout the multiple cores ? 如果我必须启动一些线程,如何在多个内核中分配它们?

thanks... 谢谢...

Your java program needs to become multi-threaded, in order to take advantage of many cores. 为了利用许多核心,您的Java程序需要成为多线程的。

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. 例如,使用java.util.concurrent.Executors创建线程池,将数据项封装为Runnable,然后将Runnable提交给线程池。

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. 冒着过于简化的风险,只需让您的旧类实现Runnable,将main()中的内容放入Run()中,然后使用该类创建一个新线程并启动该线程。

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. 因此,实际上可能就这么简单。

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 . 为此,您需要做一些学习,我建议您从Java Concurrency Tutorial开始

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM