[LinkedIn] Implement a thread safe hashtable
来源:互联网 发布:淘宝直通车开车时间 编辑:程序博客网 时间:2024/06/05 04:50
/* * before entering into synchronized method or block thread needs to acquire the lock * Here different thread cannot access "put" / "get" from the same instance of hashmap at the same time * The lock is obtained at the object level, meaning if t1 is using put(), t2 cannot use get() simultaneously */public ThreadSafeHashMap() { items = (LinkedList<Cell<K, V>>[]) new LinkedList[cap]; } public int hashCode(K key) { return key.toString().length() % items.length; } public synchronized void put(K key, V value) { int x = hashCode(key); if (items[x] == null) { items[x] = new LinkedList<Cell<K, V>>(); } LinkedList<Cell<K, V>> list = items[x]; for (Cell<K, V> c : list) { if (c.equals(key)) { list.remove(c); break; } } Cell<K, V> cell = new Cell<K, V>(key, value); list.add(cell); }public synchronized V get(K key) { int x = hashCode(key); if (items[x] == null) { return null; } LinkedList<Cell<K, V>> list = items[x]; for (Cell<K, V> c : list) { if (c.equals(key)) { return c.getValue(); } } return null; }}
0 0
- [LinkedIn] Implement a thread safe hashtable
- [LinkedIn] singleton, thread safe
- [LinkedIn] Implement a Semaphore
- Making a Thread-Safe or Synchronized Collection
- How to write a thread-safe Singleton?
- Configuring a thread-safe HttpClient(MyMoviesWithHttpClient)
- Thread-safe, Signal-safe
- thread safe
- STL(VC6) Good Q&A(五):Are Containers Thread Safe?
- STL(VC6) Good Q&A(五):Are Containers Thread Safe?
- STL(VC6) Good Q&A(五):Are Containers Thread Safe?
- STL(VC6) Good Q&A(五):Are Containers Thread Safe?
- STL(VC6) Good Q&A(五):Are Containers Thread Safe?
- STL(VC6) Good Q&A(五):Are Containers Thread Safe?
- STL(VC6) Good Q&A(五):Are Containers Thread Safe?
- Implementing a Thread-Safe Queue using Condition Variables
- Implementing a Thread-Safe Queue using Condition Variables (Updated)
- Spring MVC: how to build a thread-safe Controller
- Spring mvc详解
- 递归与分治策略(1)
- 杭电 HDU 1570 A C
- hdu 3038 How Many Answers Are Wrong(带权并查集+树的性质)
- 局部变量、全局变量、堆、堆栈、静态和全局
- [LinkedIn] Implement a thread safe hashtable
- js、jquery扩展
- 解决unix与windows文本结尾不匹配的问题
- linux下eclipse中windows->preferences下看不到Hadoop MapReduce的配置项
- android edittext 点击回车会响应两次的解决方案
- OpenCv中用hough变换检测直线,有图有真相
- hdu 2052 Picture
- python-pygame 安装
- 中国人的一天