0. About mutex locks
The so-called mutex lock refers to a lock that can only be held by one thread at a time. Before jdk1.5, we usually used the synchronized mechanism to control multiple threads Access to shared resources. Now, Lock provides a wider range of locking operations than the synchronized mechanism. The main difference between Lock and synchronized mechanisms:
The synchronized mechanism provides access to implicit monitor locks associated with each object , and forces all lock acquisitions and releases to appear in a block structure. When multiple locks are acquired, they must be released in reverse order. The synchronized mechanism releases locks implicitly, as long as the thread running code exceeds Once the scope of the synchronized statement block is exceeded, the lock will be released. The Lock mechanism must explicitly call the unlock() method of the Lock object to release the lock. This means that acquiring the lock and releasing the lock do not appear in the same block structure, and to update Free ordering of locks is possible.
1. Introduction to ReentrantLock
ReentrantLock is a reentrant mutex lock, also known as "exclusive lock".
As the name suggests, the ReentrantLock lock can only be held by one thread at the same point in time; reentrant means that the ReentrantLock lock can be acquired multiple times by a single thread.
ReentrantLock is divided into "fair lock" and "unfair lock". The difference is reflected in whether the lock acquisition mechanism is fair. "Lock" is to protect competing resources and prevent errors caused by multiple threads operating threads at the same time. ReentrantLock can only be acquired by one thread at the same point in time (when a thread acquires the "lock", other threads must wait); ReentrantLock All threads that acquire the lock are managed through a FIFO waiting queue. Under the "fair lock" mechanism, threads queue up to acquire locks in sequence; while in "unfair lock", when the lock is available for acquisition, the threads will acquire the lock regardless of whether they are at the beginning of the queue.
ReentrantLock function list
// 创建一个 ReentrantLock ,默认是“非公平锁”。 ReentrantLock() // 创建策略是fair的 ReentrantLock。fair为true表示是公平锁,fair为false表示是非公平锁。 ReentrantLock(boolean fair) // 查询当前线程保持此锁的次数。 int getHoldCount() // 返回目前拥有此锁的线程,如果此锁不被任何线程拥有,则返回 null。 protected Thread getOwner() // 返回一个 collection,它包含可能正等待获取此锁的线程。 protected Collection<Thread> getQueuedThreads() // 返回正等待获取此锁的线程估计数。 int getQueueLength() // 返回一个 collection,它包含可能正在等待与此锁相关给定条件的那些线程。 protected Collection<Thread> getWaitingThreads(Condition condition) // 返回等待与此锁相关的给定条件的线程估计数。 int getWaitQueueLength(Condition condition) // 查询给定线程是否正在等待获取此锁。 boolean hasQueuedThread(Thread thread) // 查询是否有些线程正在等待获取此锁。 boolean hasQueuedThreads() // 查询是否有些线程正在等待与此锁有关的给定条件。 boolean hasWaiters(Condition condition) // 如果是“公平锁”返回true,否则返回false。 boolean isFair() // 查询当前线程是否保持此锁。 boolean isHeldByCurrentThread() // 查询此锁是否由任意线程保持。 boolean isLocked() // 获取锁。 void lock() // 如果当前线程未被中断,则获取锁。 void lockInterruptibly() // 返回用来与此 Lock 实例一起使用的 Condition 实例。 Condition newCondition() // 仅在调用时锁未被另一个线程保持的情况下,才获取该锁。 boolean tryLock() // 如果锁在给定等待时间内没有被另一个线程保持,且当前线程未被中断,则获取该锁。 boolean tryLock(long timeout, TimeUnit unit) // 试图释放此锁。 void unlock()
2. ReentrantLock example
By comparing "Example 1" and "Example 2", we can clearly understand the functions of lock and unlock
2.1 Example 1
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; // LockTest1.java // 仓库 class Depot { private int size; // 仓库的实际数量 private Lock lock; // 独占锁 public Depot() { this.size = 0; this.lock = new ReentrantLock(); } public void produce(int val) { lock.lock(); try { size += val; System.out.printf("%s produce(%d) --> size=%d\n", Thread.currentThread().getName(), val, size); } finally { lock.unlock(); } } public void consume(int val) { lock.lock(); try { size -= val; System.out.printf("%s consume(%d) <-- size=%d\n", Thread.currentThread().getName(), val, size); } finally { lock.unlock(); } } }; // 生产者 class Producer { private Depot depot; public Producer(Depot depot) { this.depot = depot; } // 消费产品:新建一个线程向仓库中生产产品。 public void produce(final int val) { new Thread() { public void run() { depot.produce(val); } }.start(); } } // 消费者 class Customer { private Depot depot; public Customer(Depot depot) { this.depot = depot; } // 消费产品:新建一个线程从仓库中消费产品。 public void consume(final int val) { new Thread() { public void run() { depot.consume(val); } }.start(); } } public class LockTest1 { public static void main(String[] args) { Depot mDepot = new Depot(); Producer mPro = new Producer(mDepot); Customer mCus = new Customer(mDepot); mPro.produce(60); mPro.produce(120); mCus.consume(90); mCus.consume(150); mPro.produce(110); } }
Running results:
Thread-0 produce(60) --> size=60 Thread-1 produce(120) --> size=180 Thread-3 consume(150) <-- size=30 Thread-2 consume(90) <-- size=-60 Thread-4 produce(110) --> size=50
Result analysis:
(1) Depot is a warehouse. Goods can be produced in the warehouse through produce(), and goods in the warehouse can be consumed through consume(). Mutually exclusive access to the warehouse is achieved through an exclusive lock: before operating (production/consumption) goods in the warehouse, the warehouse will be locked through lock(), and then unlocked through unlock() after the operation.
(2) Producer is the producer class. Call the produce() function in Producer to create a new thread to produce products in the warehouse.
(3) Customer is the consumer class. Call the consume() function in Customer to create a new thread to consume products in the warehouse.
(4) In the main thread main, we will create a new producer mPro and a new consumer mCus. They produce/consume products into the warehouse respectively.
According to the production/consumption quantity in main, the final remaining products in the warehouse should be 50. The running results are in line with our expectations!
There are two problems with this model:
(1) In reality, the capacity of the warehouse cannot be a negative number. However, warehouse capacity in this model can be negative, which contradicts reality!
(2) In reality, the capacity of the warehouse is limited. However, there really is no limit to the capacity in this model!
We will talk about how to solve these two problems in a moment. Now, let's look at a simple example 2; by comparing "example 1" and "example 2", we can understand the uses of lock() and unlock() more clearly.
2.2 Example 2
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; // LockTest2.java // 仓库 class Depot { private int size; // 仓库的实际数量 private Lock lock; // 独占锁 public Depot() { this.size = 0; this.lock = new ReentrantLock(); } public void produce(int val) { // lock.lock(); // try { size += val; System.out.printf("%s produce(%d) --> size=%d\n", Thread.currentThread().getName(), val, size); // } catch (InterruptedException e) { // } finally { // lock.unlock(); // } } public void consume(int val) { // lock.lock(); // try { size -= val; System.out.printf("%s consume(%d) <-- size=%d\n", Thread.currentThread().getName(), val, size); // } finally { // lock.unlock(); // } } }; // 生产者 class Producer { private Depot depot; public Producer(Depot depot) { this.depot = depot; } // 消费产品:新建一个线程向仓库中生产产品。 public void produce(final int val) { new Thread() { public void run() { depot.produce(val); } }.start(); } } // 消费者 class Customer { private Depot depot; public Customer(Depot depot) { this.depot = depot; } // 消费产品:新建一个线程从仓库中消费产品。 public void consume(final int val) { new Thread() { public void run() { depot.consume(val); } }.start(); } } public class LockTest2 { public static void main(String[] args) { Depot mDepot = new Depot(); Producer mPro = new Producer(mDepot); Customer mCus = new Customer(mDepot); mPro.produce(60); mPro.produce(120); mCus.consume(90); mCus.consume(150); mPro.produce(110); } }
(a certain time) running result:
Thread-0 produce(60) --> size=-60 Thread-4 produce(110) --> size=50 Thread-2 consume(90) <-- size=-60 Thread-1 produce(120) --> size=-60 Thread-3 consume(150) <-- size=-60
Result description:
"Example 2" removes the lock based on "Example 1". In "Example 2", the final remaining product in the warehouse is -60, not the 50 we expected. The reason is that we did not implement mutually exclusive access to the warehouse.
2.3 Example 3
In "Example 3", we use Condition to solve the two problems in "Example 1": "The capacity of the warehouse cannot be a negative number" and "The capacity of the warehouse is limited".
The solution to this problem is to use Condition. Condition needs to be used in conjunction with Lock: through the await() method in Condition, the thread can be blocked [similar to wait()]; through the signal() method of Condition, the thread can be awakened [similar to notify()].
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.Condition; // LockTest3.java // 仓库 class Depot { private int capacity; // 仓库的容量 private int size; // 仓库的实际数量 private Lock lock; // 独占锁 private Condition fullCondtion; // 生产条件 private Condition emptyCondtion; // 消费条件 public Depot(int capacity) { this.capacity = capacity; this.size = 0; this.lock = new ReentrantLock(); this.fullCondtion = lock.newCondition(); this.emptyCondtion = lock.newCondition(); } public void produce(int val) { lock.lock(); try { // left 表示“想要生产的数量”(有可能生产量太多,需多此生产) int left = val; while (left > 0) { // 库存已满时,等待“消费者”消费产品。 while (size >= capacity) fullCondtion.await(); // 获取“实际生产的数量”(即库存中新增的数量) // 如果“库存”+“想要生产的数量”>“总的容量”,则“实际增量”=“总的容量”-“当前容量”。(此时填满仓库) // 否则“实际增量”=“想要生产的数量” int inc = (size+left)>capacity ? (capacity-size) : left; size += inc; left -= inc; System.out.printf("%s produce(%3d) --> left=%3d, inc=%3d, size=%3d\n", Thread.currentThread().getName(), val, left, inc, size); // 通知“消费者”可以消费了。 emptyCondtion.signal(); } } catch (InterruptedException e) { } finally { lock.unlock(); } } public void consume(int val) { lock.lock(); try { // left 表示“客户要消费数量”(有可能消费量太大,库存不够,需多此消费) int left = val; while (left > 0) { // 库存为0时,等待“生产者”生产产品。 while (size <= 0) emptyCondtion.await(); // 获取“实际消费的数量”(即库存中实际减少的数量) // 如果“库存”<“客户要消费的数量”,则“实际消费量”=“库存”; // 否则,“实际消费量”=“客户要消费的数量”。 int dec = (size<left) ? size : left; size -= dec; left -= dec; System.out.printf("%s consume(%3d) <-- left=%3d, dec=%3d, size=%3d\n", Thread.currentThread().getName(), val, left, dec, size); fullCondtion.signal(); } } catch (InterruptedException e) { } finally { lock.unlock(); } } public String toString() { return "capacity:"+capacity+", actual size:"+size; } }; // 生产者 class Producer { private Depot depot; public Producer(Depot depot) { this.depot = depot; } // 消费产品:新建一个线程向仓库中生产产品。 public void produce(final int val) { new Thread() { public void run() { depot.produce(val); } }.start(); } } // 消费者 class Customer { private Depot depot; public Customer(Depot depot) { this.depot = depot; } // 消费产品:新建一个线程从仓库中消费产品。 public void consume(final int val) { new Thread() { public void run() { depot.consume(val); } }.start(); } } public class LockTest3 { public static void main(String[] args) { Depot mDepot = new Depot(100); Producer mPro = new Producer(mDepot); Customer mCus = new Customer(mDepot); mPro.produce(60); mPro.produce(120); mCus.consume(90); mCus.consume(150); mPro.produce(110); } }
(A certain time) running result:
Thread-0 produce( 60) --> left= 0, inc= 60, size= 60 Thread-1 produce(120) --> left= 80, inc= 40, size=100 Thread-2 consume( 90) <-- left= 0, dec= 90, size= 10 Thread-3 consume(150) <-- left=140, dec= 10, size= 0 Thread-4 produce(110) --> left= 10, inc=100, size=100 Thread-3 consume(150) <-- left= 40, dec=100, size= 0 Thread-4 produce(110) --> left= 0, inc= 10, size= 10 Thread-3 consume(150) <-- left= 30, dec= 10, size= 0 Thread-1 produce(120) --> left= 0, inc= 80, size= 80 Thread-3 consume(150) <-- left= 0, dec= 30, size= 50
More detailed explanations of mutex locks in Java multi-thread programming For articles related to the usage of the ReentrantLock class, please pay attention to the PHP Chinese website!