Java – thread safety when inserting values into HashMap in a parallel stream

I need to make an asynchronous call with a timeout of 10 seconds, and I need to do this for each element in the map The result of the asynchronous call is stored in another map Is it safe to use HashMap in this case, or do I need to use concurrentmap?

Map<String,String> x = ArrayListMultimap.create();
Map<String,Boolean> value = Maps.newHashMap();

x.keySet().paralleStream().forEach(req -> {
   try {
      Response response = getResponseForRequest(req);
      value.put(req,response.getTitle());
   } catch(TimeoutException e) {
      value.put(req,null);
   }
}

Is this thread safe? I can't understand I know another way is to create a concurrent HashMap and consider some other padding values instead of null, because concurrent map does not support null values

Solution

You can use Map () instead of Foreach() and return using collectors Tomap () terminates the mapping created by the function instead of modifying the external mapping in parallel Consider the following examples:

Map result = x.keySet()
  .parallelStream()
  .map(req -> {
    try {
      Response response = getResponseForRequest(req);
      return new AbstractMap.SimpleEntry<>(req,response.getTitle());
    } catch (TimeoutException e) {
      return new AbstractMap.SimpleEntry<>(req,null);
    }
  })
  .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey,AbstractMap.SimpleEntry::getValue));

In this example, you will return a simpleentry object that represents the key and value of each element, and after processing all the entries, you will collect them into a single map

simplistic

Holger by completely deleting abstractmap Simpleentry recommends a simpler solution:

Map result = x.keySet()
  .parallelStream()
  .collect(Collectors.toMap(Function.identity(),req -> {
    try {
      Response response = getResponseForRequest(req);
      return response.getTitle()
    } catch (TimeoutException e) {
      return null
    }
  }));

Choose any method that suits you better

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>