我正在研究通过套接字传输数据的多线程客户端/服务器应用程序。我将<?php
include("config/dbconnect.php");
date_default_timezone_set("UTC");
$id = $_POST['trans_id'];
$query = mysql_query("select * from getgift_logs where id = '".$id."'");
$row = mysql_fetch_array($query);
$topuplink = $row['topuplink'];
$trans_id = $row['trans_id'];
$clientsecret = $row['clientsecret'];
$accesstoken = $row['accesstoken'];
$lost_money = $row['lost_money'];
$status = $row['description'];
$time = date('Ymdhis',time());
$sign = md5($money."|".$trans_id."|".$time."|".$clientsecret);
$ch = curl_init();
$data = array(
'topup_money' => $lost_money,
'reference_trans_id' => $trans_id,
'description'=>$status,
'request_time'=> $time,
'sign'=>$sign
);
$postvars = '';
foreach($data as $key=>$value) {
$postvars .= $key . "=" . $value . "&";
}
$url = $topuplink."".$accesstoken;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
print_r($response);
curl_close ($ch);
?>
片段存储在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)));
调用来自一个循环,我在其中添加每个片段并在之后检查它以查看是否一切正常(并且它是)。然后我选择3个随机条目来检查它们,现在所有内容都被最后一个条目中的数据覆盖,每个条目。
是什么导致这个?目前我只用1个线程进行测试。
输出:
.put()
编辑: putFragment()代码:
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]
答案 0 :(得分:4)
看起来你使用ByteBuffer并将实际的字节数组放入地图的值。
来自ByteBuffer#array() javadoc:
修改此缓冲区的内容将导致返回的数组内容被修改,反之亦然。
因此,以后随时修改缓冲区时,所有值也会更新。
您必须使用java.util.Arrays.copyOf(byte[] src, int length)