程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java線程:新特征-鎖(下)

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

編輯:關於JAVA

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved