在多线程环境中使用HashMap [英] Using HashMap in multithreaded environment

查看:119
本文介绍了在多线程环境中使用HashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过面试问题关于JavaRevisited

我很难理解这个问题:


在多线程环境中使用HashMap有什么问题?当get()方法进入无限循环?

What’s wrong using HashMap in a multithreaded environment? When get() method go into an infinite loop?

根据我的观点,使用 HashMap 在多线程环境中,只要我们的应用程序不访问/读取正在修改创建的 HashMap 的线程,而不是简单的访问HashMap

According to my opinion, it's not a problem to use HashMap inside a multi-threaded environment, as long as our application is not accessing/reading threads which are modifying the created HashMap, rather than simply accessing the HashMap

所以在我的理解中,只要在应用程序中我们只是访问 HashMap 在多线程环境中?

So in my understanding, it's not a problem as long as in the application we are just accessing the HashMap in a multi-threaded environment?

如果我的理解是正确的,请告诉我。

Please let me know if my understanding is correct.

推荐答案


在多线程环境中使用HashMap有什么问题?当get()方法转到无限循环?

What’s wrong using HashMap in multithreaded environment? When get() method go to infinite loop?

有多个线程使用非同步集合(实际上是任何错误)这是一种无保护的方式。确定如果每个线程都有自己的 HashMap 实例那么这不是问题。如果多个线程添加到相同的 HashMap 实例而不是,那么 是一个问题同步。即使只有一个线程正在修改 HashMap 而其他线程正在从同一个地图中读取而没有同步,您将遇到问题。

What is wrong is to have multiple threads use a non-synchronized collection (really any class) in an unprotected manner. Certain if each thread had their own HashMap instance then this is not an issue. It is a problem if multiple threads are adding to the same HashMap instance without it being synchronized. Even if just 1 thread is modifying a HashMap and other threads are reading from that same map without synchronization, you will run into problems.

如果你需要在多个线程中使用相同的哈希表对象,那么你应该考虑使用 ConcurrentHashMap ,将每个访问包装到 synchronized {} 块中的HashMap ,或者使用 Collections.synchronizedMap(新的HashMap< ... >()) construct。

If you need to use the same hash table object in multiple threads then you should consider using ConcurrentHashMap, wrapping each of the accesses to the HashMap in a synchronized {} block, or making use of the Collections.synchronizedMap(new HashMap<...>()) construct.

get()进入无限循环因为其中一个线程在内存中只有 c $ c> HashMap 部分更新的视图,并且必须有某种指针循环。这是使用具有多个线程的非同步集合的危险。

The get() goes to an infinite loop because one of the threads has only a partially updated view of the HashMap in memory and there must be some sort of pointer loop. That's the peril of using an unsynchronized collection with multiple threads.


所以在我的理解中,只要在应用程序中我们只是在多线程环境中访问HashMap,这不是问题吗?

So in my understanding, it's not a problem as long as in the application we are just accessing the HashMap in a multi-threaded environment?

如果通过访问表示阅读,则确实具有资格。您必须确保:

If by "accessing" you mean "reading", then this is true with qualifications. You must make sure:


  • HashMap 的所有更新都已完成 before 线程被实例化,创建映射的线程也分叉线程

  • 线程只使用 HashMap 处于只读模式( get()或迭代而不删除)

  • 没有线程更新地图

  • All of the updates to the HashMap are completed before the threads are instantiated and the thread that creates the map also forks the threads
  • The threads are only using the HashMap in read-only mode (get() or iteration without remove)
  • There are no threads updating the map

如果这些条件中的任何一个不正确,那么您将需要使用同步地图。

If any of these conditions are not true then you will need to use a synchronized map instead.

这篇关于在多线程环境中使用HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆