플랫폼 - Java 용 스레드 안전 맵
자바 소프트웨어 플랫폼 (5)
java.util.concurrent의 메소드
ConcurrentHashMap<K,V>
ConcurrentMap<K,V>
ConcurrentNavigableMap<K,V>
ConcurrentHashMap<K,V>
ConcurrentSkipListMap<K,V>
java.util.Collections에서
Collections.synchronizedMap(Map<K,V> m)
Collections.synchronizedNavigableMap(NavigableMap<K,V> m)
Collections.synchronizedSortedMap(SortedMap<K,V> m)
https://code.i-harness.com
내가 스레드 안전지도가 필요해, 내가 이런 식으로 뭔가 : (나는 아주 새로운 자바)
public static class Manager
{
static
{
//something wrong here, doesn't compile
list = new java.util.Collections
.synchronizedMap(new Map<String, Client>());
}
static Map<String,Client> list;
public static void AddClient(Client client)
{
// thread safe add client to the list
}
public static void RemoveClient(Client client)
{
// thread safe remove client to the list
}
}
귀하의지도 "목록"정적 블록에 액세스하려는 경우 정적이어야합니다.
코드는 가져 오기를 무시하고 다음과 같이 보입니다.
public class Manager
{
Map<String,Client> list = java.util.Collections.synchronizedMap(new HashMap<String, Client>());
public void AddClient(Client client)
{
// thread safe add client to the list
}
public void RemoveClient(Client client)
{
// thread safe remove client to the list
}
}
즉, 이것은 당신이 희망하는만큼 스레드 안전하지 않다는 것을주의하십시오. 다른 사람들이 언급했듯이 Java Concurrent Collections 를 사용하려고합니다.
java.util.concurrent
패키지의 java.util.concurrent.ConcurrentHashMap 클래스는 synchronizedMap
(및 Hashtable
보다 훨씬 뛰어난 확장 성)보다 훨씬 우수한 동시성을 제공하는 Map의 스레드 안전 구현입니다. http://www.ibm.com/developerworks/java/library/j-jtp07233.html 참조 http://www.ibm.com/developerworks/java/library/j-jtp07233.html .