That is why people love EJB container then multithread problem is screened off by container.
but if with Spring, you must pay 100% attention how to use singleton. because it is not thread-safe.
You must use Thread-local to avoid this problem.
OO is important but here people make it as God,even having the religious passion on it. The real architecture needs more than OO.
In most of the situation, design pattern cannot fit the real situation. dont use pattern for creating pattern.
The only persists methodology is thought of the engineering.
public V put(K key, V value) {
if (key == null)
return putForNullKey(value);
int hash = hash(key.hashCode());
int i = indexFor(hash, table.length);
for (Entry
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(hash, key, value, i);
return null;
}
你当真敢说在多线程序情况下不同步 不会出现数据混乱,HashMap实例不会出现不可预计的异常,
如果你还坚持,那我问你HashMap与HashTable还有什么区别?
HashTable的put方法如下:
public synchronized V put(K key, V value) {
// Make sure the value is not null
if (value == null) {
throw new NullPointerException();
}
// Makes sure the key is not already in the hashtable.
Entry tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry
if ((e.hash == hash) && e.key.equals(key)) {
V old = e.value;
e.value = value;
return old;
}
}
一直很狠的说,可是 是不是需要调查一下,你印在脑子里的知道真的对吗?