zoukankan      html  css  js  c++  java
  • Java并发之FairSync和NonfairSync

    Java并发中的fairSync和NonfairSync主要区别为:

    如果当前线程不是锁的占有者,则NonfairSync并不判断是否有等待队列,直接使用compareAndSwap去进行锁的占用;

    如果当前线程不是锁的占有者,则FairSync则会判断当前是否有等待队列,如果有则将自己加到等待队列尾;

    对应的源码如下:

    FairSync (注意FairSync和NonFairSync均继承自AbstractQueuedSynchronizer):

    protected final boolean tryAcquire(int acquires) {
        final Thread current = Thread.currentThread();
        int c = getState();
        if (c == 0) {
    //判断是否有等待队列,没有队列时,进行占用,如果占用失败,将自己加到等待队列尾
    if(!hasQueuedPredecessors() &&
                compareAndSetState(0, acquires)) {
    return true;  
            } 
                setExclusiveOwnerThread(current);
                compareAndSetState(0, acquires)) {
                setExclusiveOwnerThread(current);
                return true;
            }
                compareAndSetState(0, acquires)) {
                setExclusiveOwnerThread(current);
                return true;
            }
                compareAndSetState(0, acquires)) {
                setExclusiveOwnerThread(current);
                return true;
            }
        }
        else if (current == getExclusiveOwnerThread()) {
            int nextc = c + acquires;
            if (nextc < 0)
                throw new Error("Maximum lock count exceeded");
            setState(nextc);
            return true;
        }
        return false;
    }
    

      

    NonFairSync:

    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;
    }
    

      

    占用失败后,将自己加到等待队列尾的动作在AbstractQueuedSynchronizerder类acquire方法中:
    public final void acquire(int arg) {
        if (!tryAcquire(arg) &&
            acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
            selfInterrupt();
    }
    

      



    由于FairSync和NonFairSync均继承AbstractQueuedSynchronizerder(AQS),
    这里使用了到了一个设计模式(模板模式)来设计NonFairSync和FairSync类

  • 相关阅读:
    delphi的dbgrid控件点击title排序
    在 Delphi 中使用微软全文翻译的小例子
    Google的搜索API的Delphi封装
    Delphi 自动检测U盘插入、拔出及获取U盘盘符!
    ERP开发准备
    Delphi中TStringList类常用属性方法详解
    JavaScript prototype详解
    TestNG 自动化测试入门教程--典型示例
    ucloud中的udisk错误“Read-only file system”修复指南
    websocket(二)--简单实现网页版群聊
  • 原文地址:https://www.cnblogs.com/kisf/p/8432976.html
Copyright © 2011-2022 走看看