I have one java method which contains 5 different internal methods. For performance improvement, I want to call these methods parallely.
e.g. run method1, method2, ... method5 parallel using thread.
private void getInformation() throws SQLException,
ClassNotFoundException, NamingException {
method1();
method2();
method3();
method4();
method5();
}
but all these 5 methods have different business logic.
Do something like this:
Here's a simple example:
Make sure each method does not share state (like a common mutable field in the same class) or you may get unexpected results. Oracle provides a good introduction to Java Executors. Also, this book is awesome if you are doing any kind of threading in java.
You have to use 5 different threads to execute your methods in parallel, the code it's not difficult but pretty boring.
You may want to have a look to Gpars Tasks, which make writing parallel code much more easy and enjoyable.
http://gpars.org/1.0.0/guide/guide/dataflow.html#dataflow_tasks
You can use high level concurrency instrument in Java - thread pool. But anyway you will have to create Runnable objects (tasks) and then use thread pool's method - invokeAll(). Please take a look at Oracle concurrency tutorial
take a look at java.util.concurrent http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/package-summary.html and the tutorial: http://docs.oracle.com/javase/tutorial/essential/concurrency/ basically, you will have to create an executorserice, some class extending Runnable, and invoke them
To run method1 in parallel, do following:
Do this for all your methods.
To wait method1 to finish, do
and so for all other threads.
Many people will tell you use threadpool and do not extend Thread - all this has little sense for you just now. Master this way and only then follow that advices.