简体   繁体   English

在线程中同步Hashmap

[英]Synchronizing Hashmap in Threading

I am facing some issues with synchronizing my idMap . 我在同步idMap一些问题。 This map is being used in two run() methods which are run concurrently. 此映射正在同时运行的两个run()方法中使用。 In the 1st run() method i'm simply mapping event id value) to response id (key). 在第一种run()方法中,我只是将事件ID值映射到响应ID(键)。 in the 2nd run() method I wish to obtain the event id (value) with that same response id (key). 在第二个run()方法中,我希望获得具有相同响应ID(键)的事件ID(值)。 However, at times some event id is there and at times they can't be obtained. 但是,有时存在某些事件ID,有时无法获取。 The program compiles just fine, but i'm no expert at threading and i believe threading is causing this idMap to be out of sync. 该程序编译得很好,但是我不是线程专家,我相信线程会导致此idMap不同步。 My question is simply, how can I make idMap work smoothly and obtain the event ids as I intend to? 我的问题很简单,我如何才能使idMap顺利运行​​并按idMap获取事件ID?

ConcurrentHashMap<String, String> idMap = new ConcurrentHashMap<String, String>();
ConcurrentHashMap<String, ExecutorService> executors = new ConcurrentHashMap<String, ExecutorService>();

private final class ResponderTask implements Runnable {
    private ResponderTask(Event event) {
        this.event = event;
    }
    // 1st run()    
    public void run() {
        idMap.put(response.getId(), event.getId()); 
    }
}//end ResponderTask 

private final class QuoteTask implements Runnable {
    //constructor
    //2nd run() 
    public void run() {
        IdMap.get(response.getId());
    }
}//end QuoteTask

public void onResponse(final Response response) {

    ExecutorService quoteExecutor = executors.get(response.getId());
    if (quoteExecutor == null) {
        quoteExecutor = Executors.newSingleThreadExecutor();                
        executors.put(event.getId(), quoteExecutor);            
    }
    quoteExecutor.execute(new ResponderTask(event));
}

However, at times some event id is there and at times they can't be obtained. 但是,有时存在某些事件ID,有时无法获取。 The program compiles just fine, but i'm no expert at threading and i believe threading is causing this idMap to be out of sync. 该程序编译得很好,但是我不是线程专家,我相信线程会导致此idMap不同步。

idMap is a ConcurrentHashMap which is properly synchronized and highly used and tested by many folks. idMap是一个ConcurrentHashMap ,已正确同步,并且已被许多人广泛使用和测试。 If the id is not in the map when you look it up then it has not been put in there. 如果您在查找ID时不在地图中,则说明该ID尚未放入其中。 If you explain a bit more about your code we may be able to find your problem. 如果您对代码有更多的解释,我们也许可以找到您的问题。

For example, I don't see where the response object originates from. 例如,我看不到response对象的来源。 Is it possible that the ResponseTask is processing a different response then you expect? ResponseTask是否有可能正在处理与您预期不同的响应? Is response and event supposed to be the same argument? responseevent应该是同一参数吗?

My question is simply, how can I make idMap work smoothly and obtain the event ids as I intend to? 我的问题很简单,我如何才能使idMap顺利运行并按预期获取事件ID?

It's a little hard to figure out what the proper operation of the program is supposed to be. 弄清楚该程序的正确操作应该有点困难。 If you are looking for the other thread to get the event you could use a BlockingQueue . 如果您正在寻找另一个线程来获取事件,则可以使用BlockingQueue Then your other thread can do a queue.take() which will wait until there is an event to process. 然后,您的其他线程可以执行queue.take() ,它将等待直到要处理的事件为止。 But I'm not sure what the goal is here. 但是我不确定目标是什么。

One thing that is very strange is the use of a map of ExecutorService . 非常奇怪的一件事是使用ExecutorService的映射。 Do you really need multiple of them? 您真的需要多个吗? I suspect that you really should use a single Executors.newCachedThreadPool() . 我怀疑您确实应该使用单个Executors.newCachedThreadPool() Maybe, however, you want a single thread working on all requests with the same id in which case your code should work. 但是,也许您希望单个线程处理具有相同id的所有请求,在这种情况下,您的代码应该可以工作。 I assume you are doing something like the following when you want to shutdown you application: 我想在要关闭应用程序时正在执行以下操作:

for (ExecutorService executor : executors.values()) {
    executor.shutdown();
}

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

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