Java Multitasking: Executing Single Tasks with Multiple Threads

Learn how Java multitasking allows you to perform a single task using multiple threads. Discover how to implement multitasking in Java by defining a single run() method to efficiently manage multiple threads for task execution.



Java Multithreading

Single Task by Multiple Threads

To perform a single task by many threads, define one run() method.

Example: Single Task by Multiple Threads (Extending Thread)

FileName: TestMultitasking1.java

class TestMultitasking1 extends Thread {
  public void run() {
      System.out.println("task one");
  }
  public static void main(String[] args) {
      TestMultitasking1 t1 = new TestMultitasking1();
      TestMultitasking1 t2 = new TestMultitasking1();
      TestMultitasking1 t3 = new TestMultitasking1();
      
      t1.start();
      t2.start();
      t3.start();
  }
}
Output
task one
task one
task one

Example: Single Task by Multiple Threads (Implementing Runnable)

FileName: TestMultitasking2.java

class TestMultitasking2 implements Runnable {
  public void run() {
      System.out.println("task one");
  }
  public static void main(String[] args) {
      Thread t1 = new Thread(new TestMultitasking2());
      Thread t2 = new Thread(new TestMultitasking2());
      
      t1.start();
      t2.start();
  }
}
Output
task one
task one

Multiple Tasks by Multiple Threads

To perform multiple tasks by multiple threads, define multiple run() methods.

Example: Two Tasks by Two Threads (Extending Thread)

FileName: TestMultitasking3.java

class Simple1 extends Thread {
  public void run() {
      System.out.println("task one");
  }
}

class Simple2 extends Thread {
  public void run() {
      System.out.println("task two");
  }
}

public class TestMultitasking3 {
  public static void main(String[] args) {
      Simple1 t1 = new Simple1();
      Simple2 t2 = new Simple2();
      
      t1.start();
      t2.start();
  }
}
Output
task one
task two

Example: Two Tasks by Two Threads (Anonymous Classes)

FileName: TestMultitasking4.java

public class TestMultitasking4 {
  public static void main(String[] args) {
      Thread t1 = new Thread() {
          public void run() {
              System.out.println("task one");
          }
      };
      Thread t2 = new Thread() {
          public void run() {
              System.out.println("task two");
          }
      };
      
      t1.start();
      t2.start();
  }
}
Output
task one
task two

Example: Two Tasks by Two Threads (Implementing Runnable)

FileName: TestMultitasking5.java

public class TestMultitasking5 {
  public static void main(String[] args) {
      Runnable r1 = new Runnable() {
          public void run() {
              System.out.println("task one");
          }
      };
      
      Runnable r2 = new Runnable() {
          public void run() {
              System.out.println("task two");
          }
      };
      
      Thread t1 = new Thread(r1);
      Thread t2 = new Thread(r2);
      
      t1.start();
      t2.start();
  }
}
Output
task one
task two

Printing Even and Odd Numbers using Two Threads

To print even and odd numbers using two threads, use the synchronized block and notify() method.

Example: Odd and Even Number Printing

FileName: OddEvenExample.java

public class OddEvenExample {
  int counter = 1;
  static int NUM;

  public void displayOddNumber() {
      synchronized (this) {
          while (counter < NUM) {
              while (counter % 2 == 0) {
                  try {
                      wait();
                  } catch (InterruptedException ex) {
                      ex.printStackTrace();
                  }
              }
              System.out.print(counter + " ");
              counter++;
              notify();
          }
      }
  }

  public void displayEvenNumber() {
      synchronized (this) {
          while (counter < NUM) {
              while (counter % 2 == 1) {
                  try {
                      wait();
                  } catch (InterruptedException ex) {
                      ex.printStackTrace();
                  }
              }
              System.out.print(counter + " ");
              counter++;
              notify();
          }
      }
  }

  public static void main(String[] argvs) {
      NUM = 20;
      OddEvenExample oe = new OddEvenExample();

      Thread t1 = new Thread(new Runnable() {
          public void run() {
              oe.displayEvenNumber();
          }
      });

      Thread t2 = new Thread(new Runnable() {
          public void run() {
              oe.displayOddNumber();
          }
      });

      t1.start();
      t2.start();
  }
}
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20