How to run different methods parallely

To run method1 in parallel, do following:

Thread t1=new Thread() {
   public void run() {
       method1();
   }
};
t1.start();

Do this for all your methods.

To wait method1 to finish, do

t1.join();

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.


Do something like this:

  1. For each method, create a Callable object that wraps that method.
  2. Create an Executor (a fixed thread pool executor should be fine).
  3. Put all your Callables in a list and invoke them with the Executor.

Here's a simple example:

public void testThread()
{

   //create a callable for each method
   Callable<Void> callable1 = new Callable<Void>()
   {
      @Override
      public Void call() throws Exception
      {
         method1();
         return null;
      }
   };

   Callable<Void> callable2 = new Callable<Void>()
   {
      @Override
      public Void call() throws Exception
      {
         method2();
         return null;
      }
   };

   Callable<Void> callable3 = new Callable<Void>()
   {
      @Override
      public Void call() throws Exception
      {
         method3();
         return null;
      }
   };

   //add to a list
   List<Callable<Void>> taskList = new ArrayList<Callable<Void>>();
   taskList.add(callable1);
   taskList.add(callable2);
   taskList.add(callable3);

   //create a pool executor with 3 threads
   ExecutorService executor = Executors.newFixedThreadPool(3);

   try
   {
      //start the threads and wait for them to finish
      executor.invokeAll(taskList);
   }
   catch (InterruptedException ie)
   {
      //do something if you care about interruption;
   }

}

private void method1()
{
   System.out.println("method1");
}

private void method2()
{
   System.out.println("method2");
}

private void method3()
{
   System.out.println("method3");
}

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.


I had similar requirement. On a specific operation, I had to call some set of validator methods which in-turn validates certain components. Every validator used to take certain amount of time and had to reduce it and decided to call it in async.

Indeed there are many ways to achieve it, this is one of the approach which I solved.

Since validators mostly doesn't return any values, took advantage of Runnable class lambda. In below example addition, multiply and subtraction methods will be invoked asynchronously and in parallel.

public class MultiThreading {

        public static void addition() {
            System.out.println("Addition");
        }

        public static void multiply() {
            System.out.println("multiplication");
        }

        public static void subtraction() {
            System.out.println("subtraction");
        }

        public static void main(String[] args) {
            ExecutorService executor = Executors.newCachedThreadPool();

            Runnable callMultiply = () -> multiply();  //Create Runnable reference using lambda
            executor.execute(callMultiply);
            executor.execute(() -> addition()); //inline
            executor.execute(() -> subtraction());
            
            executor.shutdown();
        }
    }