java線程——鎖(上)

在Java5中,專門提供了鎖對象,利用鎖能夠方便的實現資源的封鎖,用來控制對競爭資源併發訪問的控制,這些內容主要集中在java.util.concurrent.locks 包下面,裏面有三個重要的接口Condition、Lock、ReadWriteLock。java

 

Condition Condition 將 Object 監視器方法(waitnotify 和 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 + 
                                '}'; 
        } 
}ide

 

張三正在操做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 0this

 

從上面的輸出能夠看到,利用鎖對象太方便了,比直接在某個不知情的對象上用鎖清晰多了。spa

 

但必定要注意的是,在獲取了鎖對象後,用完後應該儘快釋放鎖,以便別的等待該鎖的線程有機會去執行。.net

本文出自 「熔 巖」 博客線程

相關文章
相關標籤/搜索