阿里筆試題手寫線程安全的LRU緩存


筆試題目:
1) 實現一個KV型的LRU(最近最少使用)Cache,支持get和put方法;要求實現時間復雜度,O(1); 
2) 如果要求get,put方法線程安全,又該如何支持?請用代碼實現,並說明如此實現的性能優缺點,語言不限;

/*
public class LRUCache {
  public String get(String key){
  }
  public void put(String key, String value){
  }
}
*/

public class LRUCache extends LinkedHashMap<String,String>{
   
    private int cache_size;

    public LRUCache(int capacity){
      super(capacity, 0.75f, true);
      this.cache_size = capacity;

    }

    public String get(String key){
      synchronized (LRUCache.class) {
        return super.getOrDefault(key, "");
      }
    }  

    public String put(String key, String value){
      synchronized (LRUCache.class) {
        return super.put(key, value);
      }
    }  

    @Override
    protected boolean removeEldestEntry(Map.Entry<String, String> eldest) {
      return this.size() > cache_size;
    }  
}

后來面試官又要求我用單例模式寫一個,沒辦法,那就來一個吧

public class LRUCache extends LinkedHashMap<String, String> {

    private int cache_size;

    private static LRUCache instance;

    private LRUCache(int capacity) {
        super(capacity, 0.75f, true);
        this.cache_size = capacity;

    }

    public static synchronized LRUCache getInstance() {
        if (instance == null) {
            instance = new LRUCache(100);
        }
        return instance;
    }

    public String get(String key) {
        synchronized (this) {
            return super.getOrDefault(key, "");
        }
    }

    public String put(String key, String value) {
        synchronized (this) {
            return super.put(key, value);
        }
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<String, String> eldest) {
        return this.size() > cache_size;
    }
}

說明:

1、LRUCache采用單例模式(緩存對象只有一個),私有構造方法,synchronized同步靜態方法獲取實例對象,保證創建實例時線程安全。
2、get和put方法中使用synchronized同步代碼塊的方式來解決get和put方法的線程安全的問題。
3、由於LRUCache是單例,synchronized(this)同步代碼塊是能保證線程安全的,但是get和put方法在多線程並發場景下會降低並發性能。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM