简体   繁体   English

Java ConcurrentHashMap覆盖所有条目

[英]Java ConcurrentHashMap overrides all entries

I'm working on a multi threaded client/server application which transfers data over sockets. 我正在研究通过套接字传输数据的多线程客户端/服务器应用程序。 I store the byte[] fragments in a ConcurrentHashMap<Integer, byte[]> and all seems to work fine until the last .put() overrides all the entries with that last one. 我将byte[]片段存储在ConcurrentHashMap<Integer, byte[]> ,直到最后一个.put()覆盖最后一个的所有条目为止,所有这些似乎都可以正常工作。

   ...
   tf.putFragment(i, readBuffer.array());
   System.out.println("check added frag " + i + " : " + Arrays.toString(tf.getFragment(i)));

   }
barrier.await();
System.out.println("check added frag " + 5 + " : " + Arrays.toString(tf.getFragment(5)));
System.out.println("check added frag " + 10 + " : " + Arrays.toString(tf.getFragment(10)));

The first .put() call from the snippet is from a loop where I add each fragment and check it afterwards to see if everything is fine (and it is). 片段中的第一个.put()调用来自一个循环,在该循环中,我添加了每个片段,然后对其进行检查,以查看一切是否正常(正确)。 Afterwards I select 3 random entries to check them and now everything is overridden with the data from the last put, each entry. 然后,我选择3个随机条目进行检查,现在所有内容都被最后一个放置项(每个条目)中的数据覆盖。

What could cause this ? 是什么原因造成的? At the moment I am testing with only 1 thread. 目前,我仅使用1个线程进行测试。

Output: 输出:

downloaded frag 821 : [48, 48, 48, 53, 48, 55, 49, 49, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 53, 49, 52, 50, 55, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 53, 49, 54, 54, 54, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 55, 50, 51, 56, 51, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 55, 51, 48, 51, 48, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 55, 51, 50, 55, 48, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 56, 56, 53, 52, 49]
check added frag 821 : [48, 48, 48, 53, 48, 55, 49, 49, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 53, 49, 52, 50, 55, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 53, 49, 54, 54, 54, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 55, 50, 51, 56, 51, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 55, 51, 48, 51, 48, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 55, 51, 50, 55, 48, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 56, 56, 53, 52, 49]
downloaded frag 822 : [32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 56, 57, 49, 56, 50, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 56, 57, 52, 50, 48, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 49, 48, 52, 48, 48, 54, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 49, 48, 52, 48, 50, 56, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 49, 48, 52, 48, 55, 57, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 116, 114, 97, 105, 108, 101, 114, 10, 60, 60, 32, 47, 83, 105, 122, 101, 32, 53]
check added frag 822 : [32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 56, 57, 49, 56, 50, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 48, 56, 57, 52, 50, 48, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 49, 48, 52, 48, 48, 54, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 49, 48, 52, 48, 50, 56, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 48, 48, 48, 48, 49, 48, 52, 48, 55, 57, 32, 48, 48, 48, 48, 48, 32, 110, 32, 10, 116, 114, 97, 105, 108, 101, 114, 10, 60, 60, 32, 47, 83, 105, 122, 101, 32, 53]
downloaded frag 823 : [54, 32, 47, 82, 111, 111, 116, 32, 51, 49, 32, 48, 32, 82, 32, 47, 73, 110, 102, 111, 32, 49, 32, 48, 32, 82, 32, 47, 73, 68, 32, 91, 32, 60, 56, 99, 97, 97, 55, 53, 52, 50, 55, 56, 101, 53, 48, 50, 97, 100, 49, 50, 102, 57, 57, 53, 50, 55, 54, 56, 56, 97, 50, 52, 57, 49, 62, 10, 60, 56, 99, 97, 97, 55, 53, 52, 50, 55, 56, 101, 53, 48, 50, 97, 100, 49, 50, 102, 57, 57, 53, 50, 55, 54, 56, 56, 97, 50, 52, 57, 49, 62, 32, 93, 32, 62, 62, 10, 115, 116, 97, 114, 116, 120, 114, 101, 102, 10, 49, 48, 52, 49, 57, 54, 10, 37, 37, 69]
check added frag 823 : [54, 32, 47, 82, 111, 111, 116, 32, 51, 49, 32, 48, 32, 82, 32, 47, 73, 110, 102, 111, 32, 49, 32, 48, 32, 82, 32, 47, 73, 68, 32, 91, 32, 60, 56, 99, 97, 97, 55, 53, 52, 50, 55, 56, 101, 53, 48, 50, 97, 100, 49, 50, 102, 57, 57, 53, 50, 55, 54, 56, 56, 97, 50, 52, 57, 49, 62, 10, 60, 56, 99, 97, 97, 55, 53, 52, 50, 55, 56, 101, 53, 48, 50, 97, 100, 49, 50, 102, 57, 57, 53, 50, 55, 54, 56, 56, 97, 50, 52, 57, 49, 62, 32, 93, 32, 62, 62, 10, 115, 116, 97, 114, 116, 120, 114, 101, 102, 10, 49, 48, 52, 49, 57, 54, 10, 37, 37, 69]
check added frag 5 : [54, 32, 47, 82, 111, 111, 116, 32, 51, 49, 32, 48, 32, 82, 32, 47, 73, 110, 102, 111, 32, 49, 32, 48, 32, 82, 32, 47, 73, 68, 32, 91, 32, 60, 56, 99, 97, 97, 55, 53, 52, 50, 55, 56, 101, 53, 48, 50, 97, 100, 49, 50, 102, 57, 57, 53, 50, 55, 54, 56, 56, 97, 50, 52, 57, 49, 62, 10, 60, 56, 99, 97, 97, 55, 53, 52, 50, 55, 56, 101, 53, 48, 50, 97, 100, 49, 50, 102, 57, 57, 53, 50, 55, 54, 56, 56, 97, 50, 52, 57, 49, 62, 32, 93, 32, 62, 62, 10, 115, 116, 97, 114, 116, 120, 114, 101, 102, 10, 49, 48, 52, 49, 57, 54, 10, 37, 37, 69]
check added frag 10 : [54, 32, 47, 82, 111, 111, 116, 32, 51, 49, 32, 48, 32, 82, 32, 47, 73, 110, 102, 111, 32, 49, 32, 48, 32, 82, 32, 47, 73, 68, 32, 91, 32, 60, 56, 99, 97, 97, 55, 53, 52, 50, 55, 56, 101, 53, 48, 50, 97, 100, 49, 50, 102, 57, 57, 53, 50, 55, 54, 56, 56, 97, 50, 52, 57, 49, 62, 10, 60, 56, 99, 97, 97, 55, 53, 52, 50, 55, 56, 101, 53, 48, 50, 97, 100, 49, 50, 102, 57, 57, 53, 50, 55, 54, 56, 56, 97, 50, 52, 57, 49, 62, 32, 93, 32, 62, 62, 10, 115, 116, 97, 114, 116, 120, 114, 101, 102, 10, 49, 48, 52, 49, 57, 54, 10, 37, 37, 69]
check added frag 15 : [54, 32, 47, 82, 111, 111, 116, 32, 51, 49, 32, 48, 32, 82, 32, 47, 73, 110, 102, 111, 32, 49, 32, 48, 32, 82, 32, 47, 73, 68, 32, 91, 32, 60, 56, 99, 97, 97, 55, 53, 52, 50, 55, 56, 101, 53, 48, 50, 97, 100, 49, 50, 102, 57, 57, 53, 50, 55, 54, 56, 56, 97, 50, 52, 57, 49, 62, 10, 60, 56, 99, 97, 97, 55, 53, 52, 50, 55, 56, 101, 53, 48, 50, 97, 100, 49, 50, 102, 57, 57, 53, 50, 55, 54, 56, 56, 97, 50, 52, 57, 49, 62, 32, 93, 32, 62, 62, 10, 115, 116, 97, 114, 116, 120, 114, 101, 102, 10, 49, 48, 52, 49, 57, 54, 10, 37, 37, 69]

EDIT: putFragment() code : 编辑:putFragment()代码:

public void putFragment(int i,byte[] f) {
    fragments.put(i, f);
}

it looks like you using a ByteBuffer and are putting the actual byte array into the value of the map. 看起来您在使用ByteBuffer并将实际的字节数组放入映射的值中。

From the ByteBuffer#array() javadoc : ByteBuffer#array() javadoc:

Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa. 对该缓冲区内容的修改将导致返回数组的内容被修改,反之亦然。

So anytime you modify the Buffer later, all the values will get updated too. 因此,以后每次修改Buffer时,所有值也会被更新。

You have to make a copy of the buffer using java.util.Arrays.copyOf(byte[] src, int length) 您必须使用java.util.Arrays.copyOf(byte[] src, int length)复制缓冲区。

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

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