Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Explain in detail the difference between Java threads and processes

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "detailed explanation of the difference between Java thread and process". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "detailed understanding of the difference between Java thread and process".

I. the difference between threads and processes

Let's talk briefly about the concept of threads and processes:

(1) A process is an application that runs in memory. For example, in a Windows system, a running exe is a process.

(2) Thread refers to an execution process in a process.

Difference:

A program has at least one process, and a process has at least one thread. An application can start multiple processes at the same time. For example, for IE browser programs, every time an IE browser window is opened, a new process is started. Thread refers to an execution process in a process, a process can have multiple threads, and each thread performs different tasks separately. When multiple threads in the process are running at the same time, this mode of operation is called concurrent running.

In addition, there is a very important difference between threads and processes: each process has an independent memory unit during execution, while multiple threads in the same process share memory, which greatly improves the running efficiency of the program.

Second, the understanding of multithreading concurrency

Multithreaded concurrency is only superficial and perceived concurrency, not essential concurrency. For a thread to run, it must occupy the CPU, and most of the computers we use today are single-CPU, so at most one thread can get the CPU and run at a time.

The essence of multithreading is "limited use of CPU resources". When one thread does not need to take up CPU but only needs to deal with resources such as CPU O, it gives other threads a chance to get CPU resources. This is a bit similar to the "overall planning method", such as let you clean the house and boil water, to do these two things well in the shortest time, you must think of first boil the water, and then in the free time to clean the house, rather than first clean and then boil the water, not first boil the water and then clean, in this example, you are the only CPU, and boiling water and cleaning are two threads.

Although there is only one CPU, it frequently switches between multiple threads. When the switching frequency is high to a certain extent, we feel that all threads are running at the same time, so we feel that these multiple threads are concurrent. Therefore, concurrency doesn't really mean that multiple threads are running at the same time, it just describes a phenomenon. Just like saying that some people are "iron men", it is just used to describe someone who is not afraid of hardship or tiredness, like an "iron man".

1. Concurrency: in an operating system, it means that several programs in a period of time are between starting and running, and these programs are all running on the same processor. Two kinds of concurrency relations are synchronization and mutual exclusion.

two。 Mutual exclusion: the mutually exclusive use of critical resources between processes is called mutual exclusion.

3. Synchronization: the relationship between processes is not the relationship of mutually exclusive critical resources, but the relationship of interdependence. Further explanation: that is, the output of the previous process is used as the input of the latter process, and the second process must wait when * processes have no output. The information sent to each other by a group of concurrent processes with a synchronous relationship is called a message or event.

Among them, there are pseudo concurrency and true concurrency, pseudo concurrency refers to the concurrency of single-core processors, and true concurrency refers to the concurrency of multi-core processors.

4. Parallelism: in a multiprogramming system with a single processor, processes are executed alternately, showing a concurrent external specialty; in multiprocessor systems, processes can not only be executed alternately, but also overlap. Only programs on multiprocessors can be processed in parallel. Thus it can be seen that parallelism is for multiprocessors. Parallelism is multiple concurrent events that occur at the same time, with the meaning of concurrency, but concurrency is not necessarily parallel, which also means that concurrent events do not necessarily occur at the same time.

5. Multithreading: multithreading is the logical layer concept of programming. It is a piece of code that runs concurrently in a process. Multithreading can realize the switching between threads.

6. Async: asynchronism and synchronization are relative, synchronization is sequential execution, after the execution of one and then the next, we need to wait and coordinate the operation. Asynchronism means being independent of each other and continuing to do your own thing while waiting for an event. You don't have to wait for the event to finish before you work. Threads are one way to achieve asynchrony. Asynchronism is so that the main thread that calls the method does not have to wait for another thread to finish synchronously, thus allowing the main thread to do something else.

Asynchronism and multithreading are not the same relationship, asynchrony is the ultimate goal, multithreading is just a means for us to achieve asynchronism. Async is when a call request is sent to the callee, and the caller can do something else without waiting for its result to return. Asynchrony can be implemented using multithreading technology or handed over to another process.

In order to better understand the above concepts, to give a simple example, suppose I want to do three things: boiling water, lifting 100 barbells, and washing clothes.

In the matter of boiling water, what I have to do is to prepare to boil the water for 1 minute, wait for the boiling water to boil for 8 minutes, and turn off the boiling water machine for 1 minute.

What I'm going to do is lift the barbell 100 for 10 minutes.

What I have to do is prepare to do laundry for 1 minute, wait for the boiling water to boil for 5 minutes, and turn off the washing machine for 1 minute.

In the case of single core

To complete the synchronization, the time I need to do is 1 + 8 + 1 + 10 + 1 + 5 + 1 = 27 minutes

If it's asynchronous, I can switch to something else while I'm waiting.

Prepare to boil water (1) + prepare to wash clothes (1) + lift 50 barbells (5) minutes + turn off the washing machine for 1 minute + 20 barbells (2) minutes + turn off the water boilers for 1 minute + 30 barbells (3) minutes

1 "1" 5 "1" 2 "1" 3 = 14 minutes

Dual-core asynchronous parallel

Nuclear No. 1 is ready to boil water for 1 minute + lift the barbell 50 times (5) minutes + wait 3 minutes + turn off the water heater for 1 minute.

Nuclear 2 is ready to wash clothes for 1 minute + lift the barbell 50 times (5) minutes + turn off the washing machine for 1 minute + wait 3 minutes

In fact, it only took 1 "5" 3 "1 = 10 minutes.

Among them, both cores waited for 3 minutes.

Dual-core asynchronous non-parallel

Nuclear 1 lift the barbell 100 times (10) minutes

Nuclear 2 prepare to boil water for 1 minute + prepare to wash clothes for 1 minute + wait for 5 minutes + + turn off the water boil for 1 minute + wait for 1 minute + turn off the washing machine for 1 minute

In fact, it only took 1 "5" 3 "1 = 10 minutes.

The practice of multithreading

Under the single core

Thread 1 is ready to boil the water for 1 minute, wait for the boiling water to boil for 8 minutes, and turn off the water heater for 1 minute.

Thread 2 raises the barbell for 100 minutes for 10 minutes

Thread 3 is ready to wash clothes for 1 minute, wait for the boiling water to boil for 5 minutes, and turn off the washing machine for 1 minute

Cpu may switch in this way. The most ideal way to switch.

Thread 1 is ready to boil water 1 sleep 1 sleep 5 sleep 1 sleep 2 turn off boiling water for 1 minute exit

Thread 2 sleep1 sleep1 lift barbell 50 5 minutes sleep1 lift barbell 20 2 minutes sleep1 lift barbell 30 minutes 3 minutes

Thread 3 sleep 1 is ready to wash clothes 1 minute sleep 5 turn off the washing machine 1 minute exit

* 14 minutes is the same as async.

But it is actually different, because the thread will not run as we expected, and if thread 2 raises the barbell and runs first, the speed of the whole process will come down.

The difference between asynchrony and synchronization is that synchronization does not cut off while io is waiting, which is a waste of time.

If it is an exclusive cpu business, such as barbell lifting business, there is no difference between multi-line and single-line in the case of single core.

The benefits of multithreading make it easier to realize the idea of asynchronous switching, because asynchronous programs are difficult to write. Multithreading itself is completed synchronously, but it should be said that

Efficiency is not as good as asynchronism. And multi-lines are easy to write and relatively efficient.

The advantage of multi-core is that you can do things at the same time, which is completely different from a single core.

Thank you for your reading, the above is the content of "detailed explanation of the difference between Java threads and processes". After the study of this article, I believe you have a deeper understanding of the difference between Java threads and processes, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report