什么是java公平锁

linjiqin 阅读:109 2022-12-27 15:29:36 评论:0

1、公平锁是指线程获取锁的顺序按照加锁的顺序分配,先来先得,先进先出。

2、公平锁可以保证线程按时间顺序执行,避免饥饿。但是公平锁的效率很低,因为要保证顺序执行,就要保持有序的队列。

实例

   /** 
     * 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 
        } 
    }

本文参考链接:https://www.yisu.com/zixun/616003.html
标签:java
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号