In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "what is deadlock, live lock, hunger and no lock in java multithreading". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Deadlock, live lock and hunger are the problems of running blocking obstacles about whether multithreads are active or not. if these three conditions occur in the thread, that is, the thread is no longer active and can no longer execute normally.
Deadlock
Deadlock is the worst case in multithreading, in which multiple threads occupy each other's resources and wait for each other to release the lock. If there is no external intervention, these threads will always deal with the blocked false dead state and form a deadlock.
For example, classmate A snatched classmate B's pen, classmate B robbed classmate A's book, and they both occupied each other's things and asked each other to return them to themselves before returning them. In this way, the dispute kept on waiting for the other party to return it, but it could not be resolved. After the teacher knew about the matter, he asked them to return it to each other, so that they could solve the problem only with the intervention of external forces. Of course, this is just an example. They can solve it without a teacher. Unlike people, computers will always be blocked if they find out that this situation is not intervened by external forces.
Live lock
Few people should hear or understand the concept of livelock, which does exist in multithreading. Livelock is just the opposite of deadlock, deadlock is that everyone can not get resources and occupy each other's resources, while livelocks get resources but release each other and do not execute. When there is mutual humility in multi-threads, resources are actively released to other threads, so that the resource jumps between multiple threads and cannot be executed, which is called a livelock.
hunger
We know that there is thread priority in multithreaded execution, and high-priority threads can jump the queue and give priority to execution, so if the high-priority thread has been preempting the resources of the low-priority thread, so that the low-priority thread can not be executed, this is hunger. Of course, there is also a case of hunger, in which one thread keeps holding on to one resource so that other threads cannot be executed. Unlike deadlocks, hunger can still be executed in a later period of time. For example, the thread that occupied the resource ended and released the resource.
No lock
No lock, that is, no lock on the resource, that is, all threads can access and modify the same resource, but only one thread can modify it successfully. The typical characteristic of no lock is that a modification operation is carried out in a loop, and the thread will constantly try to modify the shared resource, and if there is no conflict, the modification will succeed and exit, otherwise it will continue with the next loop attempt. Therefore, if multiple threads modify the same value, one thread must be able to modify successfully, while other threads that fail to modify will keep retrying until the modification is successful. In the previous article, I introduced the CAS principle and application of JDK, that is, the implementation of lock-free.
It can be seen that no lock is a very good design, it will not have jump problems of threads, improper use of locks will certainly lead to system performance problems, although no locks can not fully replace locks, but no lock is very efficient in some situations.
This is the end of what is deadlock, Live Lock, Hunger and No Lock in java multithreading. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.