如题,看网上很多人都说这个类是线程安全的,但我看了jdk1.6 的源码,并没有发现这类的线程安全机制,这个类到底是不是安全的,如果是的话,安全机制如何实现的?
2条回答 默认 最新
- iteye_1858 2014-07-01 16:19关注
[code="java"]
public synchronized int size() {public synchronized boolean isEmpty() { public synchronized boolean contains(Object value) { public synchronized Enumeration<K> keys() { public synchronized V get(Object key) { public synchronized V put(K key, V value) { public synchronized V remove(Object key) { public synchronized void putAll(Map<? extends K, ? extends V> t) { public synchronized void clear() { public synchronized Object clone() { public synchronized String toString() { private transient volatile Set<K> keySet = null; private transient volatile Set<Map.Entry<K,V>> entrySet = null; private transient volatile Collection<V> values = null; public Set<Map.Entry<K,V>> entrySet() { if (entrySet==null) entrySet = Collections.synchronizedSet(new EntrySet(), this); return entrySet; }
[/code]
你再自己去查一下 synchronized,volatile,Collections.synchronizedSet 的用法吧。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报