Java线程:新特点-锁(上)[Java编程]
本文“Java线程:新特点-锁(上)[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
在Java5中,专门供应了锁对象,操纵锁可以便利的实现资源的封闭,用来掌握对竞争资源并发拜候的掌握,这些内容主要集合在 java.util.concurrent.locks 包下面,里面有三个重要的接口Condition、Lock、ReadWriteLock.
Condition | Condition 将 Object 监督器办法(wait、notify 和 notifyAll)分化成截然差别的对象,以便通过将这些对象与肆意 Lock 实现组合利用,为每个对象供应多个等候 set (wait-set). |
Lock | Lock 实现供应了比利用 synchronized 办法和语句可得到的更遍及的锁定操作. |
ReadWriteLock | ReadWriteLock 保护了一对相关的锁定,一个用于只读操作,另一个用于写入操作. |
有关锁的介绍,API文档讲解很多,看得很烦,还是看个例子再看文档对比简单理解.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Java线程:锁
*
* @author leizhimin 2009-11-5 10:57:29
*/
public class Test {
public static void main(String[] args) {
//成立并发拜候的账户
MyCount myCount = new MyCount("95599200901215522", 10000);
//成立一个锁对象
Lock lock = new ReentrantLock();
//成立一个线程池
ExecutorService pool = Executors.newCachedThreadPool();
//成立一些并发拜候用户,一个信誉卡,存的存,取的取,好热烈啊
User u1 = new User("张三", myCount, -4000, lock);
User u2 = new User("张三他爹", myCount, 6000, lock);
User u3 = new User("张三他弟", myCount, -8000, lock);
User u4 = new User("张三", myCount, 800, lock);
//在线程池中履行各个用户的操作
pool.execute(u1);
pool.execute(u2);
pool.execute(u3);
pool.execute(u4);
//关闭线程池
pool.shutdown();
}
}
/**
* 信誉卡的用户
*/
class User implements Runnable {
private String name; //用户名
private MyCount myCount; //所要操作的账户
private int iocash; //操作的金额,当然有正负之分了
private Lock myLock; //履行操作所需的锁对象
User(String name, MyCount myCount, int iocash, Lock myLock) {
this.name = name;
this.myCount = myCount;
this.iocash = iocash;
this.myLock = myLock;
}
public void run() {
//获得锁
myLock.lock();
//履行现金业务
System.out.println(name + "正在操作" + myCount + "账户,金额为" + iocash + ",当前金额为" + myCount.getCash());
myCount.setCash(myCount.getCash() + iocash);
System.out.println(name + "操作" + myCount + "账户成功,金额为" + iocash + ",当前金额为" + myCount.getCash());
//释放锁,不然别的线程没有机会履行了
myLock.unlock();
}
}
/**
* 信誉卡账户,可随便透支
*/
class MyCount {
private String oid; //账号
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 +
'}';
}
}
张三正在操作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
从上面的输出可以看到,操纵锁对象太便利了,比直接在某个不知情的对象上用锁清楚多了.
但一定要注意的是,在获得了锁对象后,用完后应当尽快释放锁,以便别的等候该锁的线程有机会去履行.
出处:http://lavasoft.blog.51cto.com/62575/222084
以上是“Java线程:新特点-锁(上)[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |