在 HashMap 中延迟加载和缓存对象的方法是否应该同步?
这个方法应该同步吗?我想我不明白上下文切换是如何(以及何时)发生的,所以我不知道是否有可能多个线程进入我的方法内的 if 块。
public class ServiceLocator {
private static final Map<Class, Object> applicationServices =
new HashMap<Class, Object>();
/**
* Locates an application scoped service. The service is lazy loaded and
* will be cached permanently.
*
* @param type The type of service to locate.
* @return An application scoped service of the specified type.
*/
@SuppressWarnings({"unchecked"})
public synchronized static <T> T getApplicationService(Class<T> type) {
if(!applicationServices.containsKey(type)) {
// If this method is NOT synchronized, is it possible for more than
// one thread to get into this if block?
T newService = ServiceLoader.create(type);
applicationServices.put(type, newService);
return newService;
}
return (T) applicationServices.get(type);
}
}
Should this method be synchronized? I guess I don't understand how (and when) context switches can occur, so I don't know if it's possible for more than one thread to get into the if block inside my method.
public class ServiceLocator {
private static final Map<Class, Object> applicationServices =
new HashMap<Class, Object>();
/**
* Locates an application scoped service. The service is lazy loaded and
* will be cached permanently.
*
* @param type The type of service to locate.
* @return An application scoped service of the specified type.
*/
@SuppressWarnings({"unchecked"})
public synchronized static <T> T getApplicationService(Class<T> type) {
if(!applicationServices.containsKey(type)) {
// If this method is NOT synchronized, is it possible for more than
// one thread to get into this if block?
T newService = ServiceLoader.create(type);
applicationServices.put(type, newService);
return newService;
}
return (T) applicationServices.get(type);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,你说得对,这个方法需要同步。从技术上讲,上下文切换可以发生在代码中的任意两行之间,甚至可以发生在代码内部(例如,在
++i
中读取和存储i
之间)。请注意,使用
synchronized
关键字并不意味着当线程处于同步块时线程不会被中断并且不会发生上下文切换。它仅意味着尝试在其他线程中执行相同代码的任何其他线程都会阻塞,从而有效地允许拥有锁的线程继续。即使使用 ConcurrentHashMap 也无济于事,因为安全的 putIfAbsent() 方法需要给定键已有值 - 并且您可能不想每次都急于创建服务。
然而,对于这个相当常见的问题,有一些性能更好的方法,例如,请参阅:如何实现自填充 EHcache?
Yes, you are absolutely right, this method requires synchronization. Technically context switch can occur between any two lines in your code, and even inside (e.g. between reading and storing
i
in++i
).Note that using
synchronized
keyword does not mean that the thread won't be interrupted and the context switch will not occur while your thread is in synchronized block. It only means that any other thread that tries to execute the same code in other thread will block, effectively allowing the one owning the lock to continue.Even using
ConcurrentHashMap
won't help you since safeputIfAbsent()
method requires already existing value for a given key - and you probably don't want to eagerly create service each time.However there are some better performing approaches to this quite common problem, e.g. see: How to implement Self Populating EHcache?
正如其他人提到的,您需要同步以确保不会创建多个服务实例。
您应该查看用于初始化单例的双重检查锁定惯用法,以提高此处的性能。
例如,
getApplicationService
本身不需要同步
。在大多数情况下,它会在缓存中找到服务,并且仅当您在映射中找不到该元素时才需要锁定:As others have mentioned you need synchronization to ensure that you don't create multiple instances of service.
You should look at the double checked locking idiom used for initializing singltons to improve performance here.
For example,
getApplicationService
itself does not need to besynchronized
. In most cases it will find the service in cache and locking is only required when you don't find the element in the map:synchronized
是使其成为线程安全的最简单方法,但不是最有效的方法。如果没有
synchronized
,您的代码将会有两个问题:HashMap
不是线程安全的,不应该在没有同步的情况下从多个线程使用它。type
)已位于其中时,一个线程可以进入if
块,因此相同type
的两个实例将被创建。因此,如果
synchronized
的解决方案不能满足您的需求,您需要通过其他方式解决这两个问题。第一个问题可以通过使用ConcurrentHashMap来解决。第二个更复杂,可以通过将FutureTask
放入映射中来解决,例如 这个。synchronized
is the simpliest way to make it thread-safe, but not the most efficient one.Without
synchronized
your code would have 2 problems:HashMap
is not thread-safe, it should not be used from multiple threads without synchronization.if
block when another one (with the sametype
) is already inside it, thus two instances of the sametype
will be created.So, if solution with
synchronized
doesn't satisfy you, you'll need to solve these two problems by other means. The first problem can be solved by usingConcurrentHashMap
. The second one is more complex and can be solved by puttingFutureTask
s into map, something like this.@Tomasz Nurkiewicz
可以使用 putIfAbsent() 来竞争锁;第一个线程创建服务;其他线程等待通知。这样,我们就不会只为一项服务锁定整个地图,从而导致卡住。他们甚至给它起了一个奇怪的名字,谷歌“memoizer”。在某些情况下,带有 FutureTask 的 Impl 实际上存在细微的错误或不良行为。
对这样一个实用程序的需求——根据第一需求计算一些东西然后缓存它——是相当强烈的。一个好的实现并不是微不足道的,普通程序员不应该费心去重新发明它。 Java8 可能会发布类似的东西。目前人们可以使用Guava的计算地图。
@Tomasz Nurkiewicz
It's possible to use
putIfAbsent()
to compete for a lock; the first thread gets to create the service; other threads wait to be notified. This way, we don't lock the whole map just for one service, which can get stuck. They even have a weird name for it, google "memoizer". Impls with FutureTask actually have subtle bugs or undesired behaviors in certain cases.The need for such a utility - compute something on first demand then cache it - is quite strong. A good impl isn't trivial, and average programmers shouldn't bother to reinvent it. Java8 probably will ship something like it. For now people can use Guava's computing map.