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

How to understand java processes and threads

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Most people do not understand the knowledge points of this article "how to understand java processes and threads", so the editor summarizes the following contents, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to understand java processes and threads" article.

I. the meaning of processes and threads

Process: a process is the basic unit that owns resources.

Threads: threads are the basic unit of CPU scheduling.

Second, give an example

☛, for example, our mobile phone has two processes to go on, one is a note, the other is PUBG.

The ☛ note process runs the file to open the saved text, and the "open file" here we call it the resource; the PUBG process listens to the user key event, and the "listen user key event" here is called its resource.

☛ we all know that the note software has the function of auto-saving, which is executed in the T1 thread, and the user edits the text in the T2 thread; PUBG assumes that there are also two threads, T3 and T4, responsible for tilting the head of QE and shooting. The concept I gave above is that "threads are the basic unit of CPU scheduling." the basic unit of CPU scheduling is not to open notes and PUBG processes, but T1, T2, T3, T4 threads.

Illustration:

★ Note: a process must have at least one thread

☛ if a process is not multithreaded, how can you shoot while tilting your head while playing PUBG? A single thread can only do one thing, and events cannot be done concurrently, which is a waste of CPU resources. The so-called multi-process is that while PUBG is running in the background, you use notes to take notes. A process is a piece of software running, and a thread is a thread code fragment in the execution process.

Thread pool

Concept:

Why introduce the concept of thread pool when ① has multithreaded programming?

Thread pool is very simple and convenient for us developers to create, but it is a heavy burden for the server. Each user requests to log on to the software to open a thread, and there will be a lot of threads created. If you think about so many threads competing for CUP resources and continuous switching, isn't this CUP scheduling exhausting rhythm? In order to solve this problem, the concept of thread pool is introduced. Through the thread pool, a certain number of threads are created in advance, and there is no need to wait for the task to be created. The function of creating threads in advance is to execute the task directly when the system has a task. If a task comes, the thread of the thread pool will execute the task, and then return to the thread pool to wait for the next task without destroying it. The thread we are talking about is the execution of the code, created dynamically.

The creation of the ② thread is started through .start, and once the thread is started, the run method is automatically executed. So a new problem arises. The task hasn't come yet. The run method is executed directly after creating a thread in advance.

In fact, the thread pool is also important, that is, when the thread pool is first created, let it enter the blocking state and wait for the arrival of the system task; if the task comes, we will wake up the first thread student to take the teacher's task to work.

How does ③ get the thread pool clogged?

As a thread container, BlockingQueu can provide a strong guarantee for thread synchronization. BlockingQueue is easy to understand. It is a container that holds the tasks of the system. The thread of the thread pool calls its take () method to fetch the task for execution, and if the BlockingQueue does not have an executable task, the thread blocks; the thread can place the task by calling the put method, and it will block if the task is full.

Illustration:

That is to say, there must be a loop in each Run method in the thread pool to determine whether the BlockingQueue has an executable task, if so, to execute it, and then return to the BlockingQueue to obtain the task. If the BlockingQueue has no executable task, the thread is in a blocking waiting state.

The above is about the content of this article on "how to understand java processes and threads". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

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

Internet Technology

Wechat

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

12
Report