简体   繁体   English

使用Java的Hashtable进行互斥

[英]mutual exclusion using Hashtable of java

hi mate i have a global Hashtable in my class, and two thread A and B that work with her. 嗨,朋友,我班上有一个全局哈希表,还有两个与她一起工作的线程A和B。 A reads from HashTable and B write in Hashtable.. is there a problem of mutual exclusion ? 从HashTable读取A,在Hashtable中写入B。是否存在互斥的问题? i need to syncrhonize it or Hashtable class is safe ? 我需要同步它还是Hashtable类是安全的?

Hashtable is a thread-safe implementation of the Map interface. HashtableMap接口的线程安全实现。

In regular put and get operations you will be safe. 在常规的放置和获取操作中,您将很安全。 However, when you will iterate on it in one thread and modify its contents from another thread, you will have ConcurrentModificationException issues. 但是,当您在一个线程中对其进行迭代并从另一个线程修改其内容时,将遇到ConcurrentModificationException问题。 So, when iterating, make sure you iterate on a copy of the original Hashtable . 因此,迭代时,请确保迭代原始Hashtable的副本。

您应该改用ConcurrentHashMap ,它是java.util.Map接口的更好/更快的实现。

It's useful to use a synchronized HashMap offered by java collections. 使用java集合提供的同步HashMap很有用。 This class is a simple wrapper and encapsulates the hashmap : 此类是一个简单的包装器,并封装了hashmap:

Collections.synchronizedMap(new HashMap());

Further example example is in the java docs : http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html synchronizedMap 进一步的示例示例在java docs中: http : //docs.oracle.com/javase/1.4.2/docs/api/java/util/Collections.htmlsyncedMap

HashTable gaurantees that any operation executed on it is atomic. HashTable确保对其执行的任何操作都是原子的。 but If you are executing multiple operations you should synchronize them. 但是,如果要执行多个操作,则应该synchronize它们。 In below example even if contains and put are atomic but the code has a check and act raise condition so you need additional synchronization for this. 在下面的示例中,即使containsput是原子的,但代码具有check and act条件,因此您需要对此进行附加synchronization

    if(!hashtable.contains(Object))
    {
        hashtable.put(key, value);
    }

Also check Collections.synchronizedMap() or ConcurrentHashMap rather than HashTable 还要检查Collections.synchronizedMap()ConcurrentHashMap而不是HashTable

Everyone has said what can be said but this is just to complement with regards to what you call mutual exclusion . 每个人都可以说些什么,但这只是对您所说的mutual exclusion补充。 You are asking if there won't be such a problem. 您在问是否不会出现这样的问题。 A thread-safe program must ensure that that if threadA is using say a block of code block1 no other thread will access it until the thread is done. 线程安全程序必须确保,如果threadA使用的是一个代码块block1那么在该线程完成之前,其他线程将无法访问它。 So, if I understand well what you mean by mutual exclusion , yes threads accessing the same synchronized (thread-safe) shared resource are mutually exclusive because they both can't access it at the same time. 因此,如果我很好地理解mutual exclusion含义,那么访问同一同步(线程安全)共享资源的线程是互斥的,因为它们都不能同时访问它。
Java practically does much of the hard for you if you choose one of the proposed safe Map implementation. 如果选择建议的安全Map实现之一,Java实际上会为您带来很多困难。 Now if your Hashtable ( or whatever else thread-safe mao you prefer ) is a shared resource, the only thing you need to take care of is the happens-before relation. 现在,如果您的Hashtable(或您喜欢的其他任何线程安全的毛发)是共享资源,那么您唯一需要注意的就是before-before关系。 It will be important if one thread is reading data and the other writing data. 如果一个线程正在读取数据而另一线程正在写入数据,则将非常重要。
More can be found at java concurrency tutorial and java concurrent package docs 可以在Java并发教程Java并发包文档中找到更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM