Java?殻?仟蒙尢-迄?和?
扮寂:2011-02-20 leizhimin
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
|