java队列问题请教

酷酷啦啦xsx 2015-07-22 09:35:36
我socket服务器与客户端通信,使用BlockQueue来实现生产和消费

这个语句是在已连接客户端列表中查找已选择的客户端,并获取这个客户端的发送队列,将发送文本框的数据发送到BlockQueue中,也就是数据的生产者
for (StreamFactory currentStream : myMainFram.myStreamFactory) {
if(currentStream.getDescsString().equals(ClientListJPanel.getItemAt(myServer.getSelectedClient()))){
byte[] temp = NetSendArea.getText().getBytes();
for (byte a : temp) {
try {
currentStream.getMybBlockQueueTool().getTxQueue().put(a);;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("原数据"+a);
}
}
}


这个客户端里面的线程,读取发送队列是不是有数据,如果有,用这个客户端的IO流发送数据
while(true){
Byte temp = null;
try {
temp = myBlockQueueTool.getTxQueue().poll(10, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(temp != null){
System.out.println(temp.byteValue());
try {
myiInOutPutStream.getMyoutputStream().write(temp.byteValue());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
else {
break;
}
}




现在现象是,我在发送文本框输入“张雄”,这俩汉字内码是
张 D5C5 雄D0DB

程序打印的数据:
不正常的时候

原数据-43
-43
原数据-59
原数据-48
原数据-37
-48
-37
-59

正常的时候
原数据-43
原数据-59
原数据-48
原数据-37
-43
-59
-48
-37

queue好像是线程安全的,为什么会出现这个现象,从队列读取的数据顺序不是我放进去的顺序,导致发送出去的汉字不对
...全文
108 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

23,404

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧