<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    vjame

    優化代碼是無止境的
    隨筆 - 65, 文章 - 9, 評論 - 26, 引用 - 0
    數據加載中……

    高性能鎖ReentrantReadWriteLock

    多線程讀取并修改一個資源時,通常使用synchronized同步鎖。性能損失情況很嚴重。jdk5.0以后提供了新的ReentrantReadWriteLock可以提供比synchronized更高性能的并發。

    Dictionary.java
    package com.test;

    import java.util.HashMap;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantReadWriteLock;

    public class Dictionary {

        
    private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();

        
    private final Lock read = readWriteLock.readLock();

        
    private final Lock write = readWriteLock.writeLock();

        
    private HashMap<String, String> dictionary = new HashMap<String, String>();

        
    public void set(String key, String value) {
            write.lock();
            
    try {
                dictionary.put(key, value);
            } 
    finally {
                write.unlock();
            }
        }

        
    public String get(String key) {
            read.lock();
            
    try {
                
    return dictionary.get(key);
            } 
    finally {
                read.unlock();
            }
        }

        
    public String[] getKeys() {
            read.lock();
            
    try {
                String keys[] 
    = new String[dictionary.size()];
                
    return dictionary.keySet().toArray(keys);
            } 
    finally {
                read.unlock();
            }
        }

        
    public static void main(String[] args) {
            Dictionary dictionary 
    = new Dictionary();
            dictionary.set(
    "java""object oriented");
            dictionary.set(
    "linux""rulez");
            
    //dictionary.set("C++", "lanjh");
            Writer writer = new Writer(dictionary, "Mr. Writer ");
            
    //Writer writer2 = new Writer(dictionary, "Mr. Writer 2");
            Reader reader1 = new Reader(dictionary, "Mrs Reader 1");
            Reader reader2 
    = new Reader(dictionary, "Mrs Reader 2");
            Reader reader3 
    = new Reader(dictionary, "Mrs Reader 3");
            Reader reader4 
    = new Reader(dictionary, "Mrs Reader 4");
            Reader reader5 
    = new Reader(dictionary, "Mrs Reader 5");
            writer.start();
            
    //writer2.start();
            reader1.start();
            reader2.start();
            reader3.start();
            reader4.start();
            reader5.start();
        }
    }

    Reader.java
    package com.test;

    public class Reader extends Thread{
           
           
    private Dictionary dictionary = null;
           
    public Reader(Dictionary d, String threadName) {
             
    this.dictionary = d;
             
    this.setName(threadName);
           }
           
           
    private boolean runForestRun = true;

           
    public void run() {
             
    while (runForestRun) {
               String [] keys 
    = dictionary.getKeys();
               
    for (String key : keys) {
                 
    //reading from dictionary with READ LOCK
                 String value = dictionary.get(key);
                 
                 
    //make what ever you want with the value.
                 System.out.println(this.getName()+" = "+key + " : " + value);
               }
               
               
    //update every seconds
               try {
                 Thread.sleep(
    1000);
               } 
    catch (InterruptedException e) {
                 e.printStackTrace();
               }
             }
           }
           
           
    public void stopReader(){
             
    this.runForestRun = false;
             
    this.interrupt();
           }
         }



    Writer.java
    package com.test;

    public class Writer extends Thread{
           
    private boolean runForestRun = true;
           
    private Dictionary dictionary = null;
           
           
    public Writer(Dictionary d, String threadName) {
             
    this.dictionary = d;
             
    this.setName(threadName);
           }
           @Override
           
    public void run() {
             
    while (this.runForestRun) { 
               String [] keys 
    = dictionary.getKeys();
               
    for (String key : keys) {
                 String newValue 
    = getNewValueFromDatastore(key);
                 
    //updating dictionary with WRITE LOCK
                 dictionary.set(key, newValue);
                 System.out.println(
    this.getName()+" = "+key + " : " + newValue);

               }
               
               
    //update every seconds
               try {
                 Thread.sleep(
    1000);
               } 
    catch (InterruptedException e) {
                 e.printStackTrace();
               }
             }
           }
           
    public void stopWriter(){
             
    this.runForestRun = false;
             
    this.interrupt();
           }
           
    public String getNewValueFromDatastore(String key){
             
    //This part is not implemented. Out of scope of this artile
             return key;
           }
         }



    posted on 2008-12-22 14:57 lanjh 閱讀(1516) 評論(0)  編輯  收藏 所屬分類: Java App

    主站蜘蛛池模板: 四虎影视久久久免费观看| 国产精品高清全国免费观看| 手机在线看永久av片免费| 亚洲综合国产精品| 亚洲日韩人妻第一页| 日本免费A级毛一片| 美女被免费视频网站a| 亚洲色欲色欲www在线播放| 麻豆国产人免费人成免费视频| 一级毛片免费视频| 亚洲欧洲日韩国产一区二区三区| 全部免费毛片在线| 国内外成人免费视频| 一进一出60分钟免费视频| 久久久久亚洲AV成人无码网站 | 国产亚洲精品a在线观看app| 五月婷婷在线免费观看| 精品国产麻豆免费人成网站| 亚洲综合av一区二区三区不卡| 亚洲激情电影在线| 久久精品国产亚洲av影院| 久久精品国产精品亚洲蜜月| 午夜a级成人免费毛片| AV片在线观看免费| 国产免费久久精品丫丫| 特级毛片A级毛片100免费播放| 亚洲精品美女久久久久久久| 亚洲日韩国产精品第一页一区| 亚洲高清偷拍一区二区三区| 亚洲成人高清在线| 亚洲一区日韩高清中文字幕亚洲| 亚洲成a人片在线观看老师| 亚洲成AV人网址| 国产成人精品曰本亚洲79ren| 亚洲精品亚洲人成在线观看下载| 亚洲伊人成无码综合网| 亚洲综合色视频在线观看| 国产av无码专区亚洲av果冻传媒| 国产AⅤ无码专区亚洲AV| 亚洲精品无码不卡在线播放HE| 亚洲中文字幕日产乱码高清app|