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

Example Analysis of Javaweb process and Thread

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

Share

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

This article will explain in detail the example analysis of Javaweb processes and threads. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

First, understanding the process 1, thread first knowing

When an executable program runs, it can be seen as a process.

The process is also the smallest unit for the operating system to allocate resources.

A process corresponds to a PCB object in the kernel

A thread is an execution flow. Each thread can execute its own code according to the message. Multiple pieces of code are executed "simultaneously" between multiple threads.

2, process attributes

Process id: identity representation

A set of memory pointers: points to where the instructions and data that the process depends on are located in memory

Process status: running or hibernating

Process priority: whether this process takes precedence over CPU execution or on top of it

Context of the process: saves the progress of the last process execution on CPU so that execution can continue the next time CPU on the process

Accounting information of the process: it records how long the process has been executed on the CPU. This time is used to restrict that one process should not occupy the CPU for too long, causing other processes to fail to execute.

From a microscopic point of view, a CPU can only execute one thread command at a time.

Too many processes and too few CPUs

Through the way of "concurrency", CPU can be scheduled quickly. Microscopically, it is still serial, but the scheduling is extremely fast. Macroscopically, it feels that multiple processes are going hand in hand.

Second, why should there be threads

First of all, concurrent programming becomes a rigid requirement.

The development of single-core CPU has encountered a bottleneck. If you want to improve your computing power, you need multicore CPU. Concurrent programming can make full use of multi-core CPU resources. Some task scenarios require "waiting for IO", and concurrent programming is needed to allow waiting time for IO to do some other work.

Second, although multi-processes can also achieve concurrent programming, threads are lighter than processes.

Creating a thread is faster than creating a process.

Destroying a thread is faster than destroying a process.

Scheduling threads is faster than scheduling processes.

Third, understand the difference between threads and processes

If you think of a process as a factory, a thread is several pipelines in the factory.

Threads are actually contained in the process.

A process may have multiple threads

Each thread has its own logic (command) to execute, and each thread is an independent "execution flow".

Some resources are shared among many threads in the same process.

Thread 1 in Linux, kernel mode and user mode

If you go to the bank to deposit money, you have to interact with the teller at the window.

The process of deposit:

1. First communicate with the teller about how much money you want to deposit and deposit it in that account

2. Give the card and money to the teller

All the above are aware of the action, which is equivalent to the execution of the code in the user mode.

3. The teller gets something to operate on the computer

The above is imperceptible to the user, which is equivalent to the kernel execution of the code.

User mode: the permissions are relatively small, and you can only perform some low-risk operations.

Kernel state: much higher than user state

2pdb PCB description thread

The kernel only knows PCB.

A thread corresponds to a PCB

A process may have more than one PCB

These three PCB correspond to the three threads of the java.exe process, and these subordinate processes are called "thread groups" in the kernel, similar to many-to-one in MySql.

5. Multiple types

To take a more vivid example, eat apples and share 100 apples

Threads: allocate resources, room + table

Process: scheduling for children to eat apples

1, single process, single thread

2, multi-process single thread

3, single process and multithreading

4, multi-process and multi-thread

5. Thread analysis in process

The maximum number of threads in a thread

1 related to the number of CPU

2, which is also related to the type of task performed by the thread

CPU-intensive: programs perform computing tasks all the time

IO-intensive: mainly for input and output operations

Multiple threads grabbing a resource may cause thread unsafety

If a thread throws an exception and does not handle the exception well, the whole process will be terminated and other threads will not work.

This is the end of this article on "sample analysis of Javaweb processes and threads". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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