In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is java fair lock". The content of 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 "what is java fair lock".
1. Fair lock means that the order in which the thread acquires the lock is allocated according to the locking order, on a first-come-first-served, first-out basis.
2. Fair locks can ensure that threads execute in chronological order and avoid hunger. However, the efficiency of fair locking is very low, because to ensure sequential execution, it is necessary to maintain an orderly queue.
Example
/ * Base of synchronization control for this lock. Subclassed * into fair and nonfair versions below. Uses AQS state to * represent the number of holds on the lock. * / abstract static class Sync extends AbstractQueuedSynchronizer {private static final long serialVersionUID =-5179523762034025860L; / * * Performs {@ link Lock#lock}. The main reason for subclassing * is to allow fast path for nonfair version. * / abstract void lock (); / * * Performs non-fair tryLock. TryAcquire is implemented in * subclasses, but both need nonfair try for trylock method. * / final boolean nonfairTryAcquire (int acquires) {final Thread current = Thread.currentThread (); int c = getState (); if (c = = 0) {if (compareAndSetState (0, acquires)) {setExclusiveOwnerThread (current); return true }} else if (current = = getExclusiveOwnerThread ()) {int nextc = c + acquires; if (nextc < 0) / / overflow throw new Error ("Maximum lock count exceeded"); setState (nextc); return true;} return false } protected final boolean tryRelease (int releases) {int c = getState ()-releases; if (Thread.currentThread ()! = getExclusiveOwnerThread ()) throw new IllegalMonitorStateException (); boolean free = false; if (c = = 0) {free = true; setExclusiveOwnerThread (null) } setState (c); return free;} protected final boolean isHeldExclusively () {/ / While we must in general read state before owner, / / we don't need to do so to check if current thread is owner return getExclusiveOwnerThread () = = Thread.currentThread ();} final ConditionObject newCondition () {return new ConditionObject () } / / Methods relayed from outer class final Thread getOwner () {return getState () = = 0? Null: getExclusiveOwnerThread ();} final int getHoldCount () {return isHeldExclusively ()? GetState (): 0;} final boolean isLocked () {return getState ()! = 0;} / * Reconstitutes the instance from a stream (that is, deserializes it). * / private void readObject (java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {s.defaultReadObject (); setState (0) / / reset to unlocked state}} Thank you for your reading. The above is the content of "what is java Fair Lock". After the study of this article, I believe you have a deeper understanding of what java Fair Lock is, and the specific usage needs to be verified in practice. 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.
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.