Java thread (19): new feature-lock (top)

Source: Internet
Author: User

In java 5, the lock object is provided specifically. The lock can be used to easily block resources and control concurrent access to competing resources. These contents are mainly concentrated in java. util. concurrent. below the locks package, there are three important interfaces: Condition, Lock, and ReadWriteLock. Condition splits the Object monitor methods (wait, policy, and policyall) into distinct objects so that they can be combined with any Lock implementation, multiple waiting sets (wait-set) are provided for each object ). The Lock implementation provides a wider range of Lock operations than the synchronized Method and statement. ReadWriteLock maintains a pair of related locks, one for read-only operations and the other for write operations. The introduction to locks involves a lot of explanation in API documentation, which is annoying. It is easier to read the documentation by looking at an example. Import java. util. concurrent. executorService; import java. util. concurrent. executors; import java. util. concurrent. locks. lock; import java. util. concurrent. locks. reentrantLock;/*** 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 lock Lock = new Reentrant Lock (); // create a thread pool ExecutorService pool = Executors. newCachedThreadPool (); // creates some concurrent access users, one credit card, the storage, and the retrieval. It's very interesting. User u1 = new User ("James", myCount,-4000, lock); User u2 = new User ("Michael Zhang", myCount, 6000, lock); User u3 = new User ("Michael Zhang", myCount,-8000, lock); User u4 = new User ("Zhang San", myCount, 800, lock); // execute pool.exe cute (u1) for each User in the thread pool ); pool.exe cute (u2); pool.exe cute (u3); pool.exe cute (u4); // close the thread pool Pool. shutdown () ;}/ *** credit card User */class User implements Runnable {private String name; // username private MyCount myCount; // account private int iocash to be operated; // The amount of the operation. Of course, there are positive and negative values: private Lock myLock; // The Lock Object User (String name, myCount myCount, int iocash, Lock myLock) {this. name = name; this. myCount = myCount; this. iocash = iocash; this. myLock = myLock;} public void run () {// obtain the lock myLock. lock (); // execute cash Service System. out. println (name + "in operation" + myCount + "account, the amount is" + iocash + ", and the current amount is" + myCount. getCash (); myCount. setCash (myCount. getCash () + iocash); System. out. println (name + "operation" + myCount + "account succeeded, the amount is" + iocash + ", and the current amount is" + myCount. getCash (); // release the lock. Otherwise, other threads have no chance to execute myLock. unlock () ;}/ *** credit card account, free overdraft */class MyCount {private String oid; // account private int cash; // 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 + '}';} Zhang San is operating on the MyCount {oid = '000000', cash = 95599200901215522} account. The amount is-10000, and the current amount is 4000. MyCount {oid = '000000', cash = 95599200901215522} account operated successfully. The amount is-6000, and the current amount is 4000 Zhang San. He is operating MyCount {oid = '000000 ', cash = 6000} account, the amount is 6000, the current amount is 6000 Zhang weitu operation MyCount {oid = '000000', cash = 95599200901215522} account succeeded, the amount is 12000, the current amount is 12000 Zhang San. His brother is operating the MyCount {oid = '000000', cash = 95599200901215522} account, and the amount is-12000, the current amount is 12000 Zhang San, his brother, MyCount {oid = '000000', cash = 95599200901215522}. The account is successfully operated, and the amount is-4000, the current amount is 4000 three. MyCount {oid = '000000' is being operated', Cash = 4000} account, the amount is 800, the current amount is 4000 three operations MyCount {oid = '000000', cash = 95599200901215522} account succeeded, the amount is 4800, the current amount is 4800 Process finished with exit code 0. From the above output, it is much easier to use the lock object, which is much clearer than simply using the lock on an unknown object. However, it must be noted that after obtaining the lock object, the lock should be released as soon as possible, so that other threads waiting for the lock have the opportunity to execute.

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.