Java的锁

一、sychronized

JAVASE1.6对其的优化,加入了偏向锁、轻量级锁、重量级锁的概念。

 

 OpenJDK的JOL可以来调试对象头信息。

1.偏向锁(‐XX:BiasedLockingStartupDelay=0该参数可以立即开启偏向锁

当只有一个线程访问同步块时,会在锁对象的对象头和栈帧中存储锁的线程id。以后该线程进入则不需要进行CAS操作。只是简单的测试一下对象头中的MarkWord中是否记录该线程id,如果有则直接获取锁成功;如果没有则看是否对象头中有偏向锁的标识,有的话使用CAS方式将偏向锁的线程id指向该线程,否则使用CAS竞争。

2.轻量级锁

多个线程在不同时间段请求同一把锁,也就是同一时间段没有锁竞争。使用轻量级锁,来避免线程的阻塞以及唤醒。

3.重量级锁

当同一时刻有两个以上的线程尝试获取锁,会将偏向锁升级为重量级锁。

4.锁消除

如果方法内锁的对象不会被别的地方用到(无逃逸),将去除锁。

5.锁粗化

当出现多次连续的加锁或解锁过程,会将多次加解锁化为一次的加锁与解锁的过程。

二、Lock

 是java并发包中的类,是基于AQS(AbstractQueueSynchronizer=Node+state),通过CAS+自旋的方式实现的。以ReentrantLock为例

  public class ReentrantLock implements Lock, java.io.Serializable {

   public ReentrantLock() {
        sync = new NonfairSync();
    }

    static final class NonfairSync extends Sync {
        private static final long serialVersionUID = 7316153563782823691L;

        /**
         * Performs lock.  Try immediate barge, backing up to normal
         * acquire on failure.
         */
        final void lock() {
            if (compareAndSetState(0, 1))
                setExclusiveOwnerThread(Thread.currentThread());
            else
                acquire(1);
        }

        protected final boolean tryAcquire(int acquires) {
            return nonfairTryAcquire(acquires);
        }
    }
}

 

public abstract class AbstractQueuedSynchronizer
    extends AbstractOwnableSynchronizer
    implements java.io.Serializable {
static final class Node {
        /** Marker to indicate a node is waiting in shared mode */
        static final Node SHARED = new Node();
        /** Marker to indicate a node is waiting in exclusive mode */
        static final Node EXCLUSIVE = null;

        /** waitStatus value to indicate thread has cancelled */
        static final int CANCELLED =  1;
        /** waitStatus value to indicate successor's thread needs unparking */
        static final int SIGNAL    = -1;
        /** waitStatus value to indicate thread is waiting on condition */
        static final int CONDITION = -2;
        /**
         * waitStatus value to indicate the next acquireShared should
         * unconditionally propagate
         */
        static final int PROPAGATE = -3;

      
        volatile int waitStatus;

        
        volatile Node prev;

       
        volatile Node next;

        
        volatile Thread thread;

      
        Node nextWaiter;

       
        final boolean isShared() {
            return nextWaiter == SHARED;
        }

       
        final Node predecessor() throws NullPointerException {
            Node p = prev;
            if (p == null)
                throw new NullPointerException();
            else
                return p;
        }

        Node() {    // Used to establish initial head or SHARED marker
        }

        Node(Thread thread, Node mode) {     // Used by addWaiter
            this.nextWaiter = mode;
            this.thread = thread;
        }

        Node(Thread thread, int waitStatus) { // Used by Condition
            this.waitStatus = waitStatus;
            this.thread = thread;
        }
    }

    private transient volatile Node head;
    private transient volatile Node tail;
    private volatile int state;

    protected final boolean compareAndSetState(int expect, int update) {
        // See below for intrinsics setup to support this
        return unsafe.compareAndSwapInt(this, stateOffset, expect, update);
    }

   private Node addWaiter(Node mode) {
        Node node = new Node(Thread.currentThread(), mode);
        // Try the fast path of enq; backup to full enq on failure
        Node pred = tail;
        if (pred != null) {
            node.prev = pred;
            if (compareAndSetTail(pred, node)) {
                pred.next = node;
                return node;
            }
        }
        enq(node);
        return node;
    }

 private void setHead(Node node) {
        head = node;
        node.thread = null;
        node.prev = null;
    }
 public final void acquire(int arg) {
        if (!tryAcquire(arg) &&
            acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
            selfInterrupt();
    }
 protected boolean tryAcquire(int arg) {
        throw new UnsupportedOperationException();
    }

 final boolean acquireQueued(final Node node, int arg) {
        boolean failed = true;
        try {
            boolean interrupted = false;
            for (;;) {
                final Node p = node.predecessor();
                if (p == head && tryAcquire(arg)) {
                    setHead(node);
                    p.next = null; // help GC
                    failed = false;
                    return interrupted;
                }
                if (shouldParkAfterFailedAcquire(p, node) &&
                    parkAndCheckInterrupt())
                    interrupted = true;
            }
        } finally {
            if (failed)
                cancelAcquire(node);
        }
    }

  /**
     * Convenience method to interrupt current thread.
     */
    static void selfInterrupt() {
        Thread.currentThread().interrupt();
    }

}

获取锁的流程

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值