The Lock interface and objects are mentioned above. Using it, the secure access to competing resources is elegantly controlled, but this kind of lock does not distinguish between read and write, and this kind of lock is called a common lock. In order to improve performance, Java provides read-write locks, using read locks for reading and write locks for writing, and flexible control, which improves the execution efficiency of the program to a certain extent.
The read-write lock in Java has an interface java.util.concurrent.locks.ReadWriteLock, and there is also a specific implementation of ReentrantReadWriteLock. For the detailed API, see the JavaAPI document.
The following example is based on the example in the article, changing the ordinary lock to a read-write lock, and adding the function of account balance query, the code is as follows:
package cn.thread; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; /** * Read-write lock * * @author Lin Jiqin * @version 1.0 2013-7-25 上午10:33:37 */ public class WriteReadLockTest { public static void main(String[] args) { WriteReadLockTest test = new WriteReadLockTest(); // Create an account with concurrent access MyCount myCount = test.new MyCount("95599200901215522", 10000 ); // Create a lock object ReadWriteLock lock = new ReentrantReadWriteLock( false ); // Create a thread Pool ExecutorService pool = Executors.newFixedThreadPool(2 ); // Create some concurrent access users, a credit card, deposit and withdraw, so lively User u1 = test.new User("Zhang San", myCount, -4000 , lock, false ); User u2 = test. new User("Zhang San's father", myCount, 6000, lock, false ); User u3 = test. new User("Zhang San's younger brother", myCount, -8000, lock, false ); User u4 = test.new User("张三", myCount, 800, lock, false); User u5 = test. new User("Zhang San's father", myCount, 0, lock, true ); // Execute the operations of each user in the thread pool pool.execute(u1); pool.execute(u2); pool.execute(u3); pool.execute(u4); pool.execute(u5); // Close the thread pool pool.shutdown(); } /** * Credit card users */ class User implements Runnable { private String name; // User name private MyCount myCount; // The account to be operated private int iocash; // The amount of the operation, of course, has positive and negative points private ReadWriteLock myLock; // Execute the operation Required lock object private boolean ischeck; // whether to query User(String name, MyCount myCount, int iocash, ReadWriteLock myLock, boolean ischeck) { this .name = name; this .myCount = myCount; this.iocash = iocash; this.myLock = myLock; this.ischeck = ischeck; } public void run() { if (ischeck) { // Acquire the read lock myLock.readLock().lock(); System.out.println( "Read:" + name + "Querying" + myCount + "Account, the current amount is " + myCount.getCash()); // Release the read lock myLock.readLock().unlock(); } else { // Get write lock myLock.writeLock().lock(); // Execute cash business System.out.println("Write: " + name + "operating" + myCount + " account, the amount is " + iocash + ", the current amount is " + myCount.getCash ()); myCount.setCash(myCount.getCash() + iocash); System.out.println( "Write: " + name + "Operation" + myCount + "The account is successful, the amount is " + iocash + ", the current amount is " + myCount.getCash()); // release the write lock myLock.writeLock().unlock(); } } } /** * Credit card account, free to overdraft */ class MyCount { private String oid; // account private int cash; // account balance MyCount(String oid, int cash) { this .oid = oid; this .cash = cash; } public String getOid() { return oid; } public void setOid(String oid) { this.oid = oid; } public int getCash() { return cash; } public void setCash(int cash) { this.cash = cash; } @Override public String toString() { return "MyCount{" + "oid='" + oid + '\'' + ", cash=" + cash + '}'; } } }
Write: Zhang San is operating MyCount{oid='95599200901215522', cash=10000} account, the amount is -4000 , and the current amount is 10000 Write: Zhang San successfully operated MyCount{oid ='95599200901215522', cash=6000} account, the amount is -4000 , and the current amount is 6000 Write: Zhang San's father is operating the MyCount{oid ='95599200901215522', cash=6000 } account, the amount is 6000, and the current amount is 6000 Write: Zhang San's father operated MyCount{oid ='95599200901215522', cash=12000 } account successfully, the amount is 6000, the current amount is 12000 Write: Zhang San is operating the MyCount{oid ='95599200901215522', cash=12000 } account, the amount is 800, and the current amount is 12000 Write: Zhang San successfully operated the MyCount{oid ='95599200901215522', cash=12800 } account, the amount is 800, and the current amount is 12800 Write: Zhang San and his brother are operating MyCount{oid ='95599200901215522', cash=12800} account, the amount is -8000 , and the current amount is 12800 Write: Zhang San and his brother successfully operated the MyCount{oid ='95599200901215522', cash=4800} account, the amount is -8000 , and the current amount is 4800 Read: Zhang San's father is inquiring MyCount{oid ='95599200901215522', cash=4800} account, the current amount is 4800
In actual development, it is better to use read-write locks when the read-write locks can be used, rather than ordinary locks, in order to achieve better performance.