Multitasking in Java


Java Multitasking ThreadViews 15126

Earlier, we have seen about multithreading in Java. In this tutorial, we will discuss multitasking in Java along with key differences between Multitasking vs Multithreading.

Multitasking in Java

Multitasking is the ability of the CPU to perform multiple tasks simultaneously. There will be continuous context switching of CPU between the tasks which is rapid and hence results in better performance. The CPU collaborates between the users and performs multiple tasks effectively. The main difference between multitasking and multithreading is that, in multitasking, each process has a different resource and memory and does not share it.

Multitasking in Java

Types of multitasking in Java

There are 2 types of multitasking in Java:

  • Process-based multitasking: More than 1 process runs concurrently. Eg: Executing an excel spreadsheet while running a word processor.
  • Thread-based multitasking: More than 1 thread executes concurrently. A thread executes different parts of the same program. Eg: Formatting a text in a word processor and printing it at the same time. Even though each thread has a different execution path for the same process, all threads share the same resource and memory.

Now let us see in detail about multitasking with different examples below.

Java Example: Execute a single task by multiple threads

Below is an example of multiple threads executing the same task by extending the Thread class. We can see that all the threads t1, t2, and t3 execute the same task specified in the run() method.

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

  public static void main(String[] args) {
    SingleTaskMultipleThreads t1 = new SingleTaskMultipleThreads();
    SingleTaskMultipleThreads t2 = new SingleTaskMultipleThreads();
    SingleTaskMultipleThreads t3 = new SingleTaskMultipleThreads();
    
    t1.start();
    t2.start();
    t3.start();
    
  }

}
Executing task
Executing task
Executing task

The below example shows multiple threads executing a single task by implementing the Runnable interface. We will get the same output as we saw in the previous example, but the only difference is in the implementation. Here, we create an instance of the Thread class and pass the anonymous object of the main class.

public class SingleTaskExecution implements Runnable {
  
  public static void main(String[] args) {
    Thread th1 = new Thread(new SingleTaskExecution());
    Thread th2 = new Thread(new SingleTaskExecution());
    
    th1.start();
    th2.start();
  }

  @Override
  public void run() {
    System.out.println("Executing task");
    
  }

}
Executing task
Executing task

In both examples, we have multiple threads performing the same task. In this case, each thread executes in a separate space called stack by sharing the same resource.

Multitasking in Java

Java Example: Executing multiple tasks by multiple threads – Multitasking

Now let’s see an example of multitasking in Java where multiple threads execute multiple tasks simultaneously by extending the Thread class. Here, each run() method denotes a different task. Each thread executes a single task which means t1 executes the run() method of Task1 class and t2 executes the run() method of Task2 class.

class Task1 extends Thread {
  public void run() {
    System.out.println("Executing task 1");
  }
}

class Task2 extends Thread {
  public void run() {
    System.out.println("Executing task 2");
  }
}

public class MultitaskingDemo{
  
  public static void main(String[] args) {
    Task1 t1 = new Task1();
    Task2 t2 = new Task2();
    
    t1.start();
    t2.start();

  }

}
Executing task 1
Executing task 2

Below is the same example of multitasking in Java by implementing the Runnable interface.

class SimpleTask1 implements Runnable {

  @Override
  public void run() {
    System.out.println("Executing task1");
    
  }
  
}

class SimpleTask2 implements Runnable {

  @Override
  public void run() {
    System.out.println("Executing task2");
    
  }
  
}
public class MultiTaskingEx {

  public static void main(String[] args) {
    Thread th1 = new Thread(new SimpleTask1());
    Thread th2 = new Thread(new SimpleTask2());
    
    th1.start();
    th2.start();

  }

}
Executing task1
Executing task2

Multitasking vs Multithreading

Below are the key difference between multitasking and multithreading.

MultitaskingMultithreading
Performs multiple tasksPerforms multiple thread execution
Each process has different resource and memory spaceAll threads share the same memory and resource
CPU context switching occurs between tasksCPU context switching occurs between threads
Performance is slow when compared to multithreadingPerformance is faster when compared to multitasking

Reference

Translate »