Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to multithread a computationally intensive code segment in Java?

I hava a java program, a section of it is compute intensive, like this

for i = 1 :512
   COMPUTE INTENSIVE SECTION
end

I want to split it into multithread, make it faster when running.

COMPUTE INTENSIVE SECTION is not sequential-wise. It means running i=1 first or i=5 fist are the same...

Can anybody give me a grand guide about this. How to do it? Thanks indeed! Happy Thanksgiving!

like image 888
Josh Morrison Avatar asked Nov 25 '10 00:11

Josh Morrison


People also ask

Can you make multiple thread to execute same instructions?

In the same multithreaded process in a shared-memory multiprocessor environment, each thread in the process can run concurrently on a separate processor, resulting in parallel execution, which is true simultaneous execution.

How do you make multiple threads dynamically in Java?

The easiest way to create a thread is to create a class that implements the Runnable interface. To execute the run() method by a thread, pass an instance of MyClass to a Thread in its constructor (A constructor in Java is a block of code similar to a method that's called when an instance of an object is created).

What are the ways to increase throughput of a multi threaded Java program?

By running instances or programs concurrently we ensure high throughput and higher performance as we can utilize the untapped resources like operating system hardware etc. For example, if a system has several CPUs, then the application can utilize these CPUs effectively and increase the throughput.


2 Answers

You should read the Concurrency Trail of the Java Tutorial. Especially Executors and Thread Pools should be relevant for you.

Basically, you create a thread pool (which is an Executor) through one of the factory methods in the Executors class and submit Runnable instances to it:

for(int i = 0; i < 512; i++){
    executor.execute(new Runnable(){public void run(){
        // your heavy code goes here
    }});
}
like image 56
Sean Patrick Floyd Avatar answered Sep 28 '22 06:09

Sean Patrick Floyd


Sounds like a thread pool would be good. Basically, you whip up a collection of N different threads, then request them in a loop. The request blocks until a thread is available.

ThreadPool pool = Executors.newFixedThreadPool(10); // 10 threads in the pool
ArrayList<Callable> collectionOfCallables = new ArrayList<Callable>( );
for (...) {
  Callable callable = new Callable<Foo>() { public Foo call() { COMPUTE INTENSIVE SECTION } }
  collectionOfCallables.add(callable);
}

ArrayList<Future<Foo>> results = pool.invokeAll( collectionOfCallables );

pool.awaitTermination(5, TimeUnit.MINUTES ); // blocks till everything is done or 5 minutes have passed.

With the Future's you really don't need to await termination. get()ing the result from a future will block until the corresponding thread is done (or canceled).

like image 41
Mark Storer Avatar answered Sep 28 '22 05:09

Mark Storer