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 threads in Java concurrent programming

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

Share

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

This article mainly explains "how to understand threads in Java concurrent programming". The content in 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 "how to understand threads in Java concurrent programming".

1. The basic concept of thread

Thread refers to multiple work units for CPU and memory in a single process. The resources of all threads in the process are shared, including global data, code execution, and so on.

Number of cpu cores and threads

Number of cores: number of threads = 1: 1

For example, an 8-core cpu, the cpu will support at least eight threads running simultaneously.

After introducing hyper-threading technology into intel:

Cores: number of threads = 1: 2

During the coding process, you can feel that there are far more threads running at the same time. Because cpu's time slice rotation mechanism is also known as RR scheduling, to put it simply, the operating system will queue the ready threads and give each process a time slice. after the thread executes this time slice in cpu, it will give up cpu resources to other threads regardless of whether it is finished or not, so it seems that there are many threads running at the same time in a certain period of time.

Operating system and cpu also need time for task switching of time slicing, and often take a large proportion of time, so pay attention to the impact of context switching on multithread execution time and performance in multithreaded development.

II. The difference between processes and threads

Process: the smallest unit in which a program runs to allocate resources. There are multiple threads in the process and the resources of this thread are shared.

Thread: the smallest unit of cpu scheduling that must exist depending on the process

Example: a jar package started is a process, and its memory size,-xmx,-xms, etc., can be configured with startup parameters. Each request is made on a thread, and cpu completes each request task by executing a thread task.

Parallelism and concurrency

Parallelism: the ability to handle things at the same time.

Concurrency: related to unit time, the ability to deal with problems in unit time

For example, assuming that hyper-threading technology is not taken into account, a 4-core cpu handles 4 threads at any one time, with a parallel number of 4, while due to the time slice rotation mechanism, it can support processing 100 threads in 1 second, and its concurrency in 1 second is 100.

Fourth, the advantage of high concurrent programming

Make full use of cpu's resources. If it is a single thread, only one core is occupied and the rest is idle; speed up the response time. Reasonable design of multithreaded programs to speed up request processing; program modularization and asynchronization

When threads share resources, there will be conflicts, deadlocks, too many starting threads, abuse of threads, and collapse of the server. Thread method, java threads are collaborative, not preemptive

Fifth, the method of thread termination interrupt ()

The thread previously provided stop (), resume (), suspend () methods to terminate the thread, but it is no longer recommended. Stop () will cause the thread to not release resources correctly, and suspend () will cause deadlock.

To interrupt the thread by interrupt (), isInterrupted (), static interrupted ()

Interrupt (): calling a thread's interrupt () method interrupts a thread, not forcibly shutting down the thread, just saying hello to the thread and setting the thread's interrupt flag position as true. Whether the thread is interrupted or not is decided by the thread itself.

IsInterrupted (): determines whether the current thread is in an interrupted state.

Static interrupted (): determines whether the current thread is in an interrupted state and changes the interrupt flag bit to false.

Thead.setPriority () method

Priority ranges from 1 to 100, with a default of 5, but thread priorities are unreliable and are not recommended as a means of thread development.

Java threads are mapped to native threads of the system, so the scheduling of threads is ultimately determined by the operating system. Although many operating systems provide the concept of thread priority, it does not necessarily correspond to the priority of java threads one by one. If there are more priorities than java threads, if there are fewer, there will have to be several cases with the same priority.

Do not rely too much on priority, which may be changed by the system itself. There is a "priority thruster" in the widows system, and its general function is that if the current system finds that a thread is executing with special "hard work", it may allocate time beyond the thread priority.

Thank you for your reading, the above is the content of "how to understand threads in Java concurrent programming". After the study of this article, I believe you have a deeper understanding of how to understand threads in Java concurrent programming. 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