熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java線程:新特征-鎖(下)

2022-06-13   來源: Java核心技術 

    在上文中提到了Lock接口以及對象使用它很優雅的控制了競爭資源的安全訪問但是這種鎖不區分讀寫稱這種鎖為普通鎖為了提高性能Java提供了讀寫鎖在讀的地方使用讀鎖在寫的地方使用寫鎖靈活控制在一定程度上提高了程序的執行效率
 
    Java中讀寫鎖有個接口ncurrentlocksReadWriteLock也有具體的實現ReentrantReadWriteLock詳細的API可以查看JavaAPI文檔
 
    下面這個例子是在文例子的基礎上將普通鎖改為讀寫鎖並添加賬戶余額查詢的功能代碼如下
 



import ncurrentExecutorService;
import ncurrentExecutors;
import ncurrentlocksReadWriteLock;
import ncurrentlocksReentrantReadWriteLock;

/**
* Java線程
*
* @author leizhimin ::
*/
public class Test {
        public static void main(String[] args) {
                //創建並發訪問的賬戶
                MyCount myCount = new MyCount( );
                //創建一個鎖對象
                ReadWriteLock lock = new ReentrantReadWriteLock(false);
                //創建一個線程池
                ExecutorService pool = ExecutorsnewFixedThreadPool();
                //創建一些並發訪問用戶一個信用卡存的存取的取好熱鬧啊
                User u = new User(張三 myCount lock false);
                User u = new User(張三他爹 myCount lock false);
                User u = new User(張三他弟 myCount lock false);
                User u = new User(張三 myCount lock false);
                User u = new User(張三他爹 myCount lock true);
                //在線程池中執行各個用戶的操作
                poolexecute(u);
                poolexecute(u);
                poolexecute(u);
                poolexecute(u);
                poolexecute(u);
                //關閉線程池
                poolshutdown();
        }
}

/**
* 信用卡的用戶
*/
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) {
                thisname = name;
                thismyCount = myCount;
                thisiocash = iocash;
                thismyLock = myLock;
                thisischeck = ischeck;
        }

        public void run() {
                if (ischeck) {
                        //獲取讀鎖
                        myLockreadLock()lock();
                        Systemoutprintln( + name + 正在查詢 + myCount + 賬戶當前金額為 + myCountgetCash());
                        //釋放讀鎖
                        myLockreadLock()unlock();
                } else {
                        //獲取寫鎖
                        myLockwriteLock()lock();
                        //執行現金業務
                        Systemoutprintln( + name + 正在操作 + myCount + 賬戶金額為 + iocash + 當前金額為 + myCountgetCash());
                        myCountsetCash(myCountgetCash() + iocash);
                        Systemoutprintln( + name + 操作 + myCount + 賬戶成功金額為 + iocash + 當前金額為 + myCountgetCash());
                        //釋放寫鎖
                        myLockwriteLock()unlock();
                }
        }
}

/**
* 信用卡賬戶可隨意透支
*/
class MyCount {
        private String oid;         //賬號
        private int cash;             //賬戶余額

        MyCount(String oid int cash) {
                thisoid = oid;
                thiscash = cash;
        }

        public String getOid() {
                return oid;
        }

        public void setOid(String oid) {
                thisoid = oid;
        }

        public int getCash() {
                return cash;
        }

        public void setCash(int cash) {
                thiscash = cash;
        }

        @Override
        public String toString() {
                return MyCount{ +
                                oid= + oid + \ +
                                 cash= + cash +
                                };
        }
}
 



張三正在操作MyCount{oid= cash=}賬戶金額為當前金額為
張三操作MyCount{oid= cash=}賬戶成功金額為當前金額為
張三他弟正在操作MyCount{oid= cash=}賬戶金額為當前金額為
張三他弟操作MyCount{oid= cash=}賬戶成功金額為當前金額為
張三正在操作MyCount{oid= cash=}賬戶金額為當前金額為
張三操作MyCount{oid= cash=}賬戶成功金額為當前金額為
張三他爹正在查詢MyCount{oid= cash=}賬戶當前金額為
張三他爹正在操作MyCount{oid= cash=}賬戶金額為當前金額為
張三他爹操作MyCount{oid= cash=}賬戶成功金額為當前金額為

Process finished with exit code
 
    在實際開發中最好在能用讀寫鎖的情況下使用讀寫鎖而不要用普通鎖以求更好的性能
 
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25525.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.