Personal profile: Bohemian, love of life. Java Cultivator (wechat official ID: Java Cultivator), welcome to follow. Access to 2000G of detailed information on the 2020 interview questions

The difference between the blocking lock and the spin lock mentioned today is that the running state of the thread is changed, so that the thread enters the blocking state for waiting. When the corresponding signal (wake up, time) is obtained, it can enter the ready state of the thread. All the threads in the ready state enter the running state through competition.

Note: A Thread has the following states: New, ready, Running, blocked, and dead

Advantages and disadvantages analysis:

Blocked threads do not consume CPU time and do not result in excessive CPU usage, but the entry time and recovery time are slightly slower than spinlocks.

In the case of intense competition, the performance of blocking lock is significantly higher than that of spin lock.

In JAVA, methods that can enter \ exit, block, or contain blocking locks include the synchronized keyword, ReentrantLock, and synchronized. Object.wait()\notify(), locksupport.park ()/unpart()(JUC(Java Util Concurrency) is often used).

PS: Well, synchronized locks are biased, lightweight, and heavyweight locks.

  • Biased lock: For a piece of synchronized code, the lock is biased in favor of the thread that acquired it for the first time. If the lock is not held by another thread during execution, the thread holding the biased lock will automatically acquire the lock without synchronization. Locking and unlocking require no additional cost, and there is only a nanosecond difference compared to implementing asynchronous methods. If there is lock contention between threads, there is additional lock cancellation cost. This applies to scenarios where only one thread accesses a synchronized block.

  • Lightweight lock: When a bias lock is held by another thread, the bias lock is upgraded to lightweight, and the other thread attempts to acquire the lock by spinning itself. Competing threads do not block, improving the response time of the program. If a thread that never gets a lock contention uses spin, it consumes CPU. Suitable for pursuit of response time, synchronous block execution speed is very fast.

  • Heavyweight lock: A lightweight lock is upgraded to a heavyweight lock when it is held by another thread. Thread contention does not use spin and does not consume CPU. The disadvantages are thread blocking and slow response time. Suitable for the pursuit of throughput, synchronous block execution speed is long.

Example code:

Clhlock1.java (this example is a modification of CLH lock)

public class CLHLock1 { public static class CLHNode { private volatile Thread isLocked; } @SuppressWarnings("unused") private volatile CLHNode tail; private static final ThreadLocal<CLHNode> LOCAL = new ThreadLocal<CLHNode>(); private static final AtomicReferenceFieldUpdater<CLHLock1, CLHNode> UPDATER = AtomicReferenceFieldUpdater.newUpdater(CLHLock1.class, CLHNode.class, "tail"); public void lock() { CLHNode node = new CLHNode(); LOCAL.set(node); CLHNode preNode = UPDATER.getAndSet(this, node); if (preNode ! = null) { preNode.isLocked = Thread.currentThread(); LockSupport.park(this); preNode = null; LOCAL.set(node); } } public void unlock() { CLHNode node = LOCAL.get(); if (! UPDATER.compareAndSet(this, node, null)) { System.out.println("unlock\t" + node.isLocked.getName()); LockSupport.unpark(node.isLocked); } node = null; }}Copy the code

Here we use the blocking lock of locksupport.unpark ().

Ideally, spin locks are used when threads are not in contention, and block locks are used when threads are in contention.