在Java5中,專門提供了鎖對象,利用鎖能夠方便的實現資源的封鎖,用來控制對競爭資源併發訪問的控制,這些內容主要集中在java.util.concurrent.locks 包下面,裏面有三個重要的接口Condition、Lock、ReadWriteLock。java
Condition:
Condition 將 Object 監視器方法(wait、notify 和 notifyAll)分解成大相徑庭的對象,以便經過將這些對象與任意 Lock 實現組合使用,爲每一個對象提供多個等待 set (wait-set)。併發
Lock:
Lock 實現提供了比使用 synchronized 方法和語句可得到的更普遍的鎖定操做。ide
ReadWriteLock:
ReadWriteLock 維護了一對相關的鎖定,一個用於只讀操做,另外一個用於寫入操做。this
有關鎖的介紹,API文檔解說不少,看得很煩,仍是看個例子再看文檔比較容易理解。spa
package cn.thread; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * * * @author 林計欽 * @version 1.0 2013-7-25 上午10:33:37 */ public class LockTest { public static void main(String[] args) { LockTest test = new LockTest(); // 建立併發訪問的帳戶 MyCount myCount = test.new MyCount("95599200901215522", 10000); // 建立一個鎖對象 Lock lock = new ReentrantLock(); // 建立一個線程池 ExecutorService pool = Executors.newCachedThreadPool(); // 建立一些併發訪問用戶,一個信用卡,存的存,取的取,好熱鬧啊 User u1 = test.new User("張三", myCount, -4000, lock); User u2 = test.new User("張三他爹", myCount, 6000, lock); User u3 = test.new User("張三他弟", myCount, -8000, lock); User u4 = test.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() { String string; if(iocash>0){ string="存款"; }else{ string="取款"; } // 獲取鎖 myLock.lock(); // 執行現金業務 System.out.println(name + "正在操做" + myCount + "帳戶," + string+"金額爲" + iocash + ",當前金額爲" + myCount.getCash()); myCount.setCash(myCount.getCash() + iocash); System.out.println(name + "操做" + myCount + "帳戶成功,"+ string + "金額爲" + iocash + ",當前金額爲" + myCount.getCash()); System.out.println("============"); // 釋放鎖,不然別的線程沒有機會執行了 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 + '\'' + ", 餘額=" + cash + '}'; } } }
張三正在操做MyCount{帳號='95599200901215522', 餘額=10000}帳戶,取款金額爲-4000,當前金額爲10000線程
張三操做MyCount{帳號='95599200901215522', 餘額=6000}帳戶成功,取款金額爲-4000,當前金額爲6000code
============orm
張三他弟正在操做MyCount{帳號='95599200901215522', 餘額=6000}帳戶,取款金額爲-8000,當前金額爲6000對象
張三他弟操做MyCount{帳號='95599200901215522', 餘額=-2000}帳戶成功,取款金額爲-8000,當前金額爲-2000接口
============
張三他爹正在操做MyCount{帳號='95599200901215522', 餘額=-2000}帳戶,存款金額爲6000,當前金額爲-2000
張三他爹操做MyCount{帳號='95599200901215522', 餘額=4000}帳戶成功,存款金額爲6000,當前金額爲4000
============
張三正在操做MyCount{帳號='95599200901215522', 餘額=4000}帳戶,存款金額爲800,當前金額爲4000
張三操做MyCount{帳號='95599200901215522', 餘額=4800}帳戶成功,存款金額爲800,當前金額爲4800
============