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

What are the knowledge points of Java multithreading concept

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

Share

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

This article mainly introduces "what are the knowledge points of the concept of Java multithreading". In daily operation, I believe that many people have doubts about the knowledge points of the concept of Java multithreading. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "what are the knowledge points of the concept of Java multithreading?" Next, please follow the editor to study!

Thread safety:

The consistency of data is ensured by locking. That is to say, when a thread accesses a certain data, the data is protected by the locking operation, and other threads cannot access it during the locking period.

Thread closure

When multiple threads access shared variables, locking is needed to ensure data synchronization, which increases the complexity of the program. One way to avoid data synchronization is not to share variables, such as using local variables and ThreadLocal

Thread scheduling

The process by which the system assigns the right to use CUP to a thread

Collaborative thread scheduling

The execution time of the thread is controlled by the thread itself. when it is finished, it actively informs the operating system to switch to another thread. The advantage is that the implementation is simple, the thread is aware of its own operation, and there is no thread synchronization problem. The disadvantage is that the thread execution time is uncontrollable, and if a thread has a problem, it may block there consistently.

Preemptive thread scheduling

The execution time of each thread is allocated by the operating system, and the switching of the thread is not determined by the thread itself (in Java, Thread.yield () can give up the execution time, but can not get the execution time) the thread execution time is controllable, and there is no thread blocking the process.

Java thread scheduling is preemptive scheduling.

You can set the priority of some threads to execute as many threads as possible (Java has a total of 10 thread priorities from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY). When two threads are in ready at the same time, the higher the priority, the easier it is to execute. But priority is not reliable, because Java threads are mapped to native threads, so thread scheduling depends on the operating system.

State transition

A thread that has not been started after the creation of a new (New)

Runnable: Runnable includes Running and Ready in the operating system. A thread in this state may be running or waiting for CPU to allocate execution time for it. After the thread is created, other threads call the thread's start method, then the thread is in the runnable thread pool and becomes runnable. As long as the CPU allocates the execution time, all the other resources needed for running have been obtained.

Wait indefinitely (Waiting): threads in this state will not be allocated CPU execution time, waiting to be displayed and woken up by other threads. If you do not set the Object.wait () method and Thread.join () method of timeout, and the LockSupport.park () method

Time-limited wait (Timed Waiting): threads in this state will not be allocated CPU execution time, but will be automatically awakened by the system after a certain period of time without the need to be awakened by the display. Such as Thread.sleep (), set the Object.wait () and Thread.join (), LockSupport.parkNanos () and LockSupport.parkUntil () methods of timeout

Blocked: the thread is blocked. The difference from waiting is that the blocking thread is waiting for an exclusive lock.

The blocking state is for some reason to give up the right to use CPU and temporarily stop execution until the thread is in the ready state.

Terminated: the thread finishes execution or exits the run () method unexpectedly, and the thread ends its life cycle

Three common situations of blocking

1. Wait blocking (waiting indefinitely): the running thread executes the wait () method, the thread releases the occupied resources, and JVM puts the thread into the waiting pool. After entering this state, the thread will not wake up automatically and must rely on other threads to call the notify () or notifyAll () method.

two。 Synchronization blocking: when a running thread acquires an object's synchronization lock, if the synchronization lock is occupied by another thread, JVM will put the thread into the lock pool. 3. Other blocking (time-limited wait): when the running thread executes the join () or sleep () method, or initiates the Ijoin O request, JVM will put the thread into the blocking state. When the sleep () state times out, join () waits for the thread to terminate or time out, the IUnip O processing completes, and the thread returns to the ready state.

At this point, the study of "what are the knowledge points of Java multithreading concept" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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