Multitasking


Multitasking

When several tasks are executed concurrently then it is called multi-tasking. The aim of multitasking is to increase the performance of the system by reducing response time. In this, the CPU switches between multiple programs to complete their execution in real time. Multi-tasking is heavy compared to multithreading.

There are 2 types of multi-tasking:

  • Process-based multitasking: When various jobs are executed together where each job is a separate independent operation is known as process based multitasking. It is best suited at OS Level
  • Thread-based multitasking: When several tasks are executed concurrently where each task is a separate independent part of the same program is known as thread-based multitasking and each independent part is called Thread. It is best suitable for the programmatic level.

How to complete the single task by various threads?

You can complete the single task by various threads by using only one run() method.

How to complete several tasks by several threads (multitasking in multithreading)?

You can complete several tasks by several threads by using multiple run() methods.
You can use multitasking to utilize the CPU. In the Multitasking process, the component includes multiprocessing. However, the process of multitasking is slow as compared to multithreading. And the termination process requires more time.
Multitasking is done if a CPU is equipped to execute multiple tasks at a time. It often includes CPU switching between the tasks so that programmers can cooperate with every program together. In multitasking, the processes experience separate memory and resources. Since multitasking includes CPU switching between the tasks quickly, Then minute time is required to shift from one user to another.

Example :

Com.knowledge2life class MultiThread extends Thread{ public void run(){ System.out.println("Running Thread Name: "+ this.currentThread().getName()); System.out.println("Running Thread Priority: "+ this.currentThread().getPriority()); } } public class Main { public static void main(String[] args) { Main multiThread1 = new Main(); multiThread1.setName("First Thread"); multiThread1.setPriority(Thread.MIN_PRIORITY); Main multiThread2 = new Main(); multiThread2.setName("Second Thread"); multiThread2.setPriority(Thread.MAX_PRIORITY); Main multiThread3 = new Main(); multiThread3.setName("Third Thread"); multiThread1.start(); multiThread2.start(); multiThread3.start(); } }

OUTPUT:

Main.java:10: error: cannot find symbol
multiThread1.setName("First Thread");
                       ^
symbol: method setName(String)
location: variable multiThread1 of type Main
Main.java:11: error: cannot find symbol
multiThread1.setPriority(Thread.MIN_PRIORITY);
                       ^
symbol: method setPriority(int)
location: variable multiThread1 of type Main
Main.java:14: error: cannot find symbol
multiThread2.setName("Second Thread");
                       ^
symbol: method setName(String)
location: variable multiThread2 of type Main
Main.java:15: error: cannot find symbol
multiThread2.setPriority(Thread.MAX_PRIORITY);
                       ^
symbol: method setPriority(int)
location: variable multiThread2 of type Main
Main.java:18: error: cannot find symbol
multiThread3.setName("Third Thread");
                       ^
symbol: method setName(String)
location: variable multiThread3 of type Main
Main.java:20: error: cannot find symbol
multiThread1.start();
                       ^
symbol: method start()
location: variable multiThread1 of type Main
Main.java:21: error: cannot find symbol
multiThread2.start();
                       ^
symbol: method start()
location: variable multiThread2 of type Main
Main.java:22: error: cannot find symbol
multiThread3.start();
                       ^
symbol: method start()
location: variable multiThread3 of type Main