奇怪的HashMap.put()行为 [英] strange HashMap.put() behaviour

查看:116
本文介绍了奇怪的HashMap.put()行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解决间歇性故障,这种故障似乎与从HashMap中删除对象有关,然后使用新密钥重新放回同一个对象。我的HashMap创建如下:

I am attempting to troubleshoot an intermittent failure that appears to be related to removing an object from a HashMap and then putting that same object back using a new key. My HashMap is created as follows:

transactions = new HashMap<Short, TransactionBase>();

重新赋值的代码如下:

The code that does the re-assignment is as follows:

transactions.remove(transaction.tran_no);
transaction.tran_no = generate_transaction_id();
transactions.put(transaction.tran_no, transaction);

我看到的间歇性行为是在此之后立即执行的代码依赖于事务对象可定位似乎无法使用新的事务标识查找事务对象。但是,在将来的某个时刻,交易可以定位。因此,在吸管方面,是否有任何异步效应来放置()或删除,可能会导致这种行为?

The intermittent behaviour that I am seeing is that code that executes immediately after this that depends upon the transaction object being locatable does not appear to find the transaction object using the new transaction id. However, at some future point, the transaction can be located. So pulling at straws, is there any sort of asynchronous effect to put() or remove that can cause this sort of behaviour?

我应该提到,我的知识,容器只被一个线程访问。我已经在他的文档中读过HashMap类不是同步的。

I should mention that, to the best of my knowledge, the container is being accessed by only one thread. I have already read in he documentation that class HashMap is not "synchronised".

推荐答案

HashMap类。只要你把东西放在那里,它就在那里。您应该进行双重检查和三重检查,以确保没有线程问题。

There are no "asynchronous" effects in the HashMap class. As soon as you put something in there, it's there. You should double- and triple- check to make sure that there are no threading issues.

我唯一能想到的另一件事是,您正在制作一份HashMap的某处。该副本显然不会受到您向原始内容中添加内容的影响,反之亦然。

The only other thing I can think of is that you're making a copy of the HashMap somewhere. The copy obviously won't be affected by you adding stuff into the original, or vice versa.

这篇关于奇怪的HashMap.put()行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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