Forcing multiple threads to use multiple CPUs when they are available

后端 未结 10 1159
后悔当初
后悔当初 2020-11-28 19:21

I\'m writing a Java program which uses a lot of CPU because of the nature of what it does. However, lots of it can run in parallel, and I have made my program multi-threade

10条回答
  •  无人及你
    2020-11-28 19:52

    The easiest thing to do is break your program into multiple processes. The OS will allocate them across the cores.

    Somewhat harder is to break your program into multiple threads and trust the JVM to allocate them properly. This is -- generally -- what people do to make use of available hardware.


    Edit

    How can a multi-processing program be "easier"? Here's a step in a pipeline.

    public class SomeStep {
        public static void main( String args[] ) {
            BufferedReader stdin= new BufferedReader( System.in );
            BufferedWriter stdout= new BufferedWriter( System.out );
            String line= stdin.readLine();
            while( line != null ) {
                 // process line, writing to stdout
                 line = stdin.readLine();
            }
        }
    }
    

    Each step in the pipeline is similarly structured. 9 lines of overhead for whatever processing is included.

    This may not be the absolute most efficient. But it's very easy.


    The overall structure of your concurrent processes is not a JVM problem. It's an OS problem, so use the shell.

    java -cp pipline.jar FirstStep | java -cp pipline.jar SomeStep | java -cp pipline.jar LastStep
    

    The only thing left is to work out some serialization for your data objects in the pipeline. Standard Serialization works well. Read http://java.sun.com/developer/technicalArticles/Programming/serialization/ for hints on how to serialize. You can replace the BufferedReader and BufferedWriter with ObjectInputStream and ObjectOutputStream to accomplish this.

提交回复
热议问题