package cn.study.concurrency.ch11; /** * 鎖分段 * @author xiaof * */ public class StripedMap { //同步策略:就是對數組進行分段上鎖,n個節點用n%LOCKS鎖保護 private static final int N_LOCKS = 16; private final Node[] buckets; private final Object[] locks; private static class Node { private String name; private Node next; private String key; private String value; public String getValue() { return value; } public void setValue(String value) { this.value = value; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } public String getKey() { return key; } public void setKey(String key) { this.key = key; } } public StripedMap(int numBuckets) { buckets = new Node[numBuckets]; //創建對應hash的鎖 locks = new Object[N_LOCKS]; for(int i = 0; i < N_LOCKS; ++ i) { locks[i] = new Object(); } } private final int hash(Object key) { //取絕對值 return Math.abs(key.hashCode() % buckets.length); } //get和clear public Object get(Object key) { int hash = hash(key); synchronized(locks[hash % N_LOCKS]) { //分段上鎖 for(Node m = buckets[hash]; m != null; m = m.next) { if(m.key.equals(key)) return m.value; } } return null; } /** * 清除所有的數據,但是沒有要求說要同時獲取全部的鎖的話,可以進行這樣的釋放操作 */ public void clear() { for(int i = 0; i < buckets.length; ++i) { synchronized(locks[i % N_LOCKS]) { buckets[i] = null; } } } }