|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
主要缺点就是:速度比较慢,没有C和C++快
Java中读写锁有个接口java.util.concurrent.locks.ReadWriteLock,也有详细的完成ReentrantReadWriteLock,具体的API能够检察JavaAPI文档。
上面这个例子是在文例子的基本上,将一般锁改成读写锁,并增加账户余额查询的功效,代码以下:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* Java线程:锁
*
* @author leizhimin 2009-11-5 10:57:29
*/
public class Test {
public static void main(String[] args) {
//创立并发会见的账户
MyCount myCount = new MyCount("95599200901215522", 10000);
//创立一个锁对象
ReadWriteLock lock = new ReentrantReadWriteLock(false);
//创立一个线程池
ExecutorService pool = Executors.newFixedThreadPool(2);
//创立一些并发会见用户,一个信誉卡,存的存,取的取,好热烈啊
User u1 = new User("张三", myCount, -4000, lock, false);
User u2 = new User("张三他爹", myCount, 6000, lock, false);
User u3 = new User("张三他弟", myCount, -8000, lock, false);
User u4 = new User("张三", myCount, 800, lock, false);
User u5 = new User("张三他爹", myCount, 0, lock, true);
//在线程池中实行各个用户的操纵
pool.execute(u1);
pool.execute(u2);
pool.execute(u3);
pool.execute(u4);
pool.execute(u5);
//封闭线程池
pool.shutdown();
}
}
/**
* 信誉卡的用户
*/
class User implements Runnable {
private String name; //用户名
private MyCount myCount; //所要操纵的账户
private int iocash; //操纵的金额,固然有正负之分了
private ReadWriteLock myLock; //实行操纵所需的锁对象
private boolean ischeck; //是不是查询
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) {
//猎取读锁
myLock.readLock().lock();
System.out.println("读:" + name + "正在查询" + myCount + "账户,以后金额为" + myCount.getCash());
//开释读锁
myLock.readLock().unlock();
} else {
//猎取写锁
myLock.writeLock().lock();
//实行现金营业
System.out.println("写:" + name + "正在操纵" + myCount + "账户,金额为" + iocash + ",以后金额为" + myCount.getCash());
myCount.setCash(myCount.getCash() + iocash);
System.out.println("写:" + name + "操纵" + myCount + "账户乐成,金额为" + iocash + ",以后金额为" + myCount.getCash());
//开释写锁
myLock.writeLock().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}账户,金额为-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
在实践开辟中,最幸亏能用读写锁的情形下利用读写锁,而不要用一般锁,以求更好的功能。
此出处:http://lavasoft.blog.51cto.com/62575/222433
但是对于JAVA技术类的学习,我觉得大课堂反而会影响自身独立思考的过程,因为上课的时候,老师讲课的速度很快为了不遗漏要点,通常会仔细的听, |
|