java的线程安全映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1792023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:41:13  来源:igfitidea点击:

thread safe map for java

java

提问by Omu

i need a thread safe map, i have something like this: (i'm very new to java)

我需要一个线程安全映射,我有这样的东西:(我对 Java 很陌生)

 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
        }

        }

回答by rtperson

Your map "list" needs to be static if you want to access it in a static block.

如果您想在静态块中访问它,您的地图“列表”需要是静态的。

回答by Paul Wagland

Your code should look like this, ignoring imports, et al.

您的代码应如下所示,忽略导入等。

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
    }
}

That said, beware that this is not as thread safe as you might hope. As others have mentioned, you probably want to use the Java Concurrent Collections.

也就是说,请注意这并不像您希望的那样线程安全。正如其他人所提到的,您可能想要使用Java Concurrent Collections

回答by Paul Tomblin

You can't initialize an object member variable in a static block. Static blocks are executed once when the class is first loaded, not once for every object of that class, whereas the variable "list" is created once for each object of the class.

您不能在静态块中初始化对象成员变量。静态块在类首次加载时执行一次,而不是为该类的每个对象执行一次,而为该类的每个对象创建一次变量“列表”。

Also, you can't instantiate a "new Map" because Map is an interface. You need to wrap the synchronizedMap around a real Map like a HashMap or a TreeMap.

此外,您无法实例化“新地图”,因为 Map 是一个接口。您需要将 synchronizedMap 包裹在真正的 Map 周围,例如 HashMap 或 TreeMap。

        {
           list = new java.util.Collections
          .synchronizedMap(new HashMap<String, Client>());
        }

回答by Pascal Thivent

The ConcurrentHashMapclass from the java.util.concurrentpackage is a thread-safe implementation of Map that offers far better concurrency than synchronizedMap(and far superior scalability over Hashtable). See http://www.ibm.com/developerworks/java/library/j-jtp07233.html.

包中的ConcurrentHashMapjava.util.concurrent是 Map 的线程安全实现,它提供了比 更好的并发性synchronizedMap(以及远优于 的可扩展性Hashtable)。请参阅http://www.ibm.com/developerworks/java/library/j-jtp07233.html

回答by ΦXoc? ? Пepeúpa ツ

from java.util.concurrent

来自 java.util.concurrent

ConcurrentHashMap<K,V>
ConcurrentMap<K,V>
ConcurrentNavigableMap<K,V>
ConcurrentHashMap<K,V>
ConcurrentSkipListMap<K,V>

from java.util.Collections

来自 java.util.Collections

Collections.synchronizedMap(Map<K,V> m)
Collections.synchronizedNavigableMap(NavigableMap<K,V> m)
Collections.synchronizedSortedMap(SortedMap<K,V> m)