In Java, multitasking is the process of executing multiple tasks at the same time; multitasking can be achieved in two ways: multiprocessing and multithreading.
This article will introduce you to multitasking in Java and let you know two ways to implement multitasking. I hope it will be helpful to you. [Related video tutorial recommendations: JavaTutorial]
From the above we know that multitasking is a process of performing multiple tasks at the same time. We can Use multitasking to maximize CPU utilization. Multitasking can be achieved in two ways: multiprocessing and multithreading. Let’s take a closer look at both methods.
Multiprocessing
Multiprocessing is process-based multitasking, it is a process that executes multiple processes at the same time the process of. (Explanation: A process is a running application)
#Multiple processing involves multiple CPUs, and each process has an address in memory. In other words, each process is allocated a separate memory area.
The process of multiprocessing is heavyweight, and the communication cost between the processes is very high; and switching from one process to another takes some time to save and load registers, memory mapping, update lists, etc. .
Multi-threading
Multi-threading is thread-based multi-tasking, it is a process that executes multiple threads at the same time the process of. (Explanation: A thread is a lightweight sub-process and is the smallest processing unit)
The main purpose of multi-threading is to execute two or more parts of the program at the same time to maximize the use of CPU resources. Because threads share the same address space, the communication cost between threads is very low; a multi-threaded program contains two or more parts that can run concurrently.
Why use multithreading instead of multiprocessing?
Multiprocessing and multithreading are both used to achieve multitasking, but why do we use multithreading instead of multiprocessing?
This is because threads use shared memory areas, they do not allocate separate memory areas to save memory, and switching up and down between threads takes less time than processes, and the communication cost between threads is lower .
Advantages of multi-threading:
1) Multi-threading will not block users because the threads are independent; we can perform multiple operations at the same time, so we can save time.
2) Threads are independent, so if an exception occurs in a single thread, it will not affect other threads.
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
The above is the detailed content of What is multitasking in java. For more information, please follow other related articles on the PHP Chinese website!