Java thread (20): new feature-lock (lower)

Source: Internet
Author: User

Java thread: the new feature-Lock (below) mentioned above the Lock interface and object. using it, it elegantly controls secure access to competing resources, but this Lock does not distinguish between read and write, the lock is called an ordinary lock. To improve the performance, Java provides read/write locks. The read locks are used in the reading area and write locks are used in the writing area for flexible control, which improves the execution efficiency of the program to a certain extent. In Java, the read/write lock has an interface java. util. concurrent. locks. ReadWriteLock, and also has a specific implementation of ReentrantReadWriteLock. For detailed APIs, you can view the JavaAPI documentation. The following example shows how to change the common lock to a read/write lock and add the account balance query function. The Code is as follows: import java. util. concurrent. executorService; import java. util. concurrent. executors; import java. util. concurrent. locks. readWriteLock; import java. util. concurrent. locks. reentrantReadWriteLock;/*** Java thread: Lock ** @ author leizhimin 2009-11-5 10:57:29 */public class Test {public static void main (String [] args) {// create an account for concurrent access MyCount myCount = new MyCount (" 95599200901215522 ", 10000); // create a lock Object ReadWriteLock lock = new ReentrantReadWriteLock (false); // create a thread pool ExecutorService pool = Executors. newFixedThreadPool (2); // creates some concurrent access users, one credit card, the storage, and the retrieval. It's very lively. User u1 = new User ("James", myCount, -4000, lock, false); User u2 = new User ("Zhang San his dad", myCount, 6000, lock, false ); user u3 = new User ("Zhang San his younger brother", myCount,-8000, lock, false); User u4 = new User ("Zhang San", myCount, 8 00, lock, false); User u5 = new User ("Zhang San his dad", myCount, 0, lock, true ); // perform the operations pool.exe cute (u1), pool.exe cute (u2), pool.exe cute (u3), pool.exe cute (u4), and pool.exe cute (u5) in the thread pool ); // close the thread pool. shutdown () ;}/ *** credit card User */class User implements Runnable {private String name; // username private MyCount myCount; // The account private int iocash to be operated; // The amount of the operation. Of course, there are positive and negative values: private ReadWriteLock myLock; // The Lock Object pr required for executing the operation Ivate 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) {// get 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. re AdLock (). unlock ();} else {// get write lock myLock. writeLock (). lock (); // execute the cash business System. out. println ("write:" + name + "operating" + myCount + "account, the amount is" + iocash + ", and the current amount is" + myCount. getCash (); myCount. setCash (myCount. getCash () + iocash); System. out. println ("write:" + name + "operation" + myCount + "account succeeded. The amount is" + iocash + ", and the current amount is" + myCount. getCash (); // release the write lock myLock. writeLock (). unlock () ;}}/ *** credit card account, free 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: James is operating on MyCount {oid = '000000', cash = 95599200901215522} account, the amount is-10000, and the current amount is 4000 write: michael Jacob's MyCount {oid = '000000', cash = 95599200901215522} account succeeded. The amount is-6000, and the current amount is 4000: michael Zhang is operating on MyCount {oid = '000000', cash = 95599200901215522} account. The amount is-6000, and the current amount is 8000: michael Jacob's brother operated on MyCount {oid = '000000', cash =-95599200901215522}, and the account was successful. The amount is-2000, and the current amount is-8000: michael is operating on MyCount {oid = '000000', cash =-95599200901215522} account, the amount is 2000, and the current amount is-800 write: Michael is operating on MyCount {oid = '000000', cash =-95599200901215522} the account is successful, the amount is 1200, and the current amount is-800 read: Michael Zhang is querying MyCount {oid = '000000 ', cash =-1200} account, the current amount is-1200 write: Michael Zhang is operating MyCount {oid = '000000', cash =-95599200901215522} account, the amount is 1200, the current amount is-1200 write: Michael Jacob's MyCount {oid = '000000', cash = 95599200901215522} account operation succeeded, the amount is 4800, the current amount is 4800 Process finished with exit code 0. In actual development, it is best to use the read/write lock when the read/write lock can be used instead of the common lock for better performance.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.