Java Thread: New feature-Lock (next)

Source: Internet
Author: User

Java read-write lock has an interface Java.util.concurrent.locks.ReadWriteLock, there are specific implementation Reentrantreadwritelock, detailed APIs can view JAVAAPI documents.

The following example is based on the text example, the normal lock changed to read and 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 a concurrent access account
Mycount mycount = new Mycount ("95599200901215522", 10000);
Create a Lock Object
Readwritelock lock = new Reentrantreadwritelock (false);
Create a thread pool
Executorservice pool = Executors.newfixedthreadpool (2);
Create a number of concurrent access users, a credit card, save, take the fetch, so lively AH
User U1 = new User ("John", Mycount, -4000, lock, false);
User U2 = New User ("Zhang San his father", Mycount, 6000, lock, false);
User U3 = New User ("Zhang San he", Mycount, -8000, lock, false);
User U4 = new User ("John", Mycount,, lock, false);
User U5 = New User ("Zhang San his father", Mycount, 0, lock, true);
Perform individual user actions 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 ();
}
}
/**
* Users of credit cards
*/
Class User implements Runnable {
private String name; User name
Private Mycount Mycount; The account to be operated on
private int iocash; The amount of the operation, of course, has positive and negative points
Private Readwritelock MyLock; The lock object required to perform the operation
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) {
Get read lock
Mylock.readlock (). Lock ();
System.out.println ("read:" + name + "is querying" + Mycount + "account, current amount is" + mycount.getcash ());
Release Read lock
Mylock.readlock (). Unlock ();
} else {
Get write Lock
Mylock.writelock (). Lock ();
Execute Cash Business
System.out.println ("write:" + name + "is 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 + "account success, the amount is" + Iocash + ", the current amount is" + mycount.getcash ());
Release Write lock
Mylock.writelock (). Unlock ();
}
}
}
/**
* Credit Card account, can be overdrawn
*/
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 +
'}';
}
}

写:张三正在操作MyCount{oid='95599200901215522', cash=10000}账户,金额为-4000,当前金额为10000
写:张三操作MyCount{oid='95599200901215522', cash=6000}账户成功,金额为-4000,当前金额为6000
写:张三他弟正在操作MyCount{oid='95599200901215522', cash=6000}账户,金额为-8000,当前金额为6000
写:张三他弟操作MyCount{oid='95599200901215522', cash=-2000}账户成功,金额为-8000,当前金额为-2000
写:张三正在操作MyCount{oid='95599200901215522', cash=-2000}账户,金额为800,当前金额为-2000
写:张三操作MyCount{oid='95599200901215522', cash=-1200}账户成功,金额为800,当前金额为-1200
读:张三他爹正在查询MyCount{oid='95599200901215522', cash=-1200}账户,当前金额为-1200
写:张三他爹正在操作MyCount{oid='95599200901215522', cash=-1200}账户,金额为6000,当前金额为-1200
写:张三他爹操作MyCount{oid='95599200901215522', cash=4800}账户成功,金额为6000,当前金额为4800
Process finished with exit code 0

In actual development, it is best to use read and write locks in the case of using read and write lock, and do not use ordinary locks, in order to better performance.

The Source: http://lavasoft.blog.51cto.com/62575/222433

Related Article

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.