Java Threads: New Features-locks (top)

Source: Internet
Author: User
Tags thread

In the JAVA5, the lock object is specially provided, and the lock can conveniently realize the resource blockade, which is used to control the control of concurrent access to competing resources, which is mainly concentrated under the Java.util.concurrent.locks package, which has three important interfaces condition, Lock, Readwritelock.

Condition Condition the object Monitor method (wait, notify, and Notifyall) into distinct objects to provide multiple waiting sets (Wait-set) for each object by using them in combination with any Lock implementation.
Lock The lock implementation provides a wider range of locking operations than is available with synchronized methods and statements.
Readwritelock Readwritelock maintains a pair of related locks, one for read-only operations and another for write operations.

Introduction to the Lock, API documentation, a lot of commentary, annoying, or look at an example to look at the document more easily understood.

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 a concurrent access account
Mycount mycount = new Mycount ("95599200901215522", 10000);
Create a Lock Object
Lock lock = new Reentrantlock ();
Create a thread pool
Executorservice pool = Executors.newcachedthreadpool ();
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);
User U2 = New User ("Zhang San his father", Mycount, 6000, lock);
User U3 = New User ("Zhang San his brother", Mycount, -8000, lock);
User U4 = new User ("John", Mycount, and Lock);
Perform individual user actions in the thread pool
Pool.execute (U1);
Pool.execute (U2);
Pool.execute (U3);
Pool.execute (U4);
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 Lock MyLock; The lock object required to perform the operation
User (String name, mycount mycount, int iocash, Lock myLock) {
THIS.name = name;
This.mycount = Mycount;
This.iocash = Iocash;
This.mylock = MyLock;
}
public void Run () {
Get lock
Mylock.lock ();
Execute Cash Business
SYSTEM.OUT.PRINTLN (name + "is operating" + Mycount + "account, the amount is" + Iocash + ", the current amount is" + mycount.getcash ());
Mycount.setcash (Mycount.getcash () + Iocash);
SYSTEM.OUT.PRINTLN (name + "Operation + Mycount +" account success, the amount is "+ Iocash +", Current amount is "+ mycount.getcash ());
Release the lock, otherwise the other thread will not be able to execute
Mylock.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}账户,金额为6000,当前金额为6000
张三他爹操作MyCount{oid='95599200901215522', cash=12000}账户成功,金额为6000,当前金额为12000
张三他弟正在操作MyCount{oid='95599200901215522', cash=12000}账户,金额为-8000,当前金额为12000
张三他弟操作MyCount{oid='95599200901215522', cash=4000}账户成功,金额为-8000,当前金额为4000
张三正在操作MyCount{oid='95599200901215522', cash=4000}账户,金额为800,当前金额为4000
张三操作MyCount{oid='95599200901215522', cash=4800}账户成功,金额为800,当前金额为4800
Process finished with exit code 0

From the above output can be seen, using the lock object is too convenient, than directly in an uninformed object with a lock more clearly.

However, it is important to note that after acquiring the lock object, you should release the lock as soon as possible, so that other threads waiting for the lock have the opportunity to execute.

Source: http://lavasoft.blog.51cto.com/62575/222084

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.