java - Android处理程序第一次无法接收消息
问题描述
我创建了两个工作线程并向UI处理程序发送消息,第一次处理程序提前从一个线程收到消息,但后来错过了第二条消息。当我再次尝试时,所有消息都可以接收。为什么第二条消息在第一次丢失?
这是 UI 处理程序:
handler=new Handler(){
@Override
public void handleMessage (Message msg)
{
switch (msg.what){
case 0:{
if(msg.arg1==10){
//process second thread
}
else
{//first thread
}
}
break;
}
super.handleMessage(msg);
}`
这是 thread_one 处理程序:
void get_list(String Msg,double price){
class runable implements Runnable{
@Override
public void run() {
Message msg=HelpedHandle.obtainMessage();
try {
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
int len =40+Msg.length;
byte Data[] =generate_Sokect_header(len,NEW_REQUIRE,id);
long msglen=Msg.length;
Data=Concat(Data,longTobyte(msglen));
Data=Concat(Data,doubleTobyte(price));
Data=Concat(Data,doubleTobyte(0));
Data=Concat(Data,Msg);
out.write(Data);
msg.what =0;
msg.arg1=-1;
}
catch (IOException e){
msg.what = -1;
}
msg.sendToTarget();
}
public byte Msg[];
public double price;
}
runable runable1=new runable();
runable1.Msg=Msg.getBytes();
runable1.price=price;
pthreadPool.execute(runable1);
}
这是线程_2:
`void get_context(){
class runable implements Runnable{
@Override
public void run() {
while(true) {
try {
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
int length = inputStream.read(recive);
byte flag[] = new byte[4];
System.arraycopy(recive, 0, flag, 0, 4);
byte len[]= new byte[8];
System.arraycopy(recive,8, len, 0, 8);
long check=byteTolong(len);
if(length!=check)
continue;
Message msg = handler.obtainMessage();
msg.what=0;
msg.arg1 = 10;
msg.setData(bundle);
msg.sendToTarget();
}
}catch{}
}
}
pthreadPool.execute(new runable());
}
解决方案
推荐阅读
- r - 如何将数据集表转换为R中的矩阵
- imagemagick - 如何在 imagemagick 的一个命令中组合一个调整大小和 3 个复合命令?
- mysql - 在承诺链中停止另一个 .then() 执行
- html - 将向上/向下箭头添加到引导手风琴
- excel - 使用一个自动电子邮件 vba 代码,允许双击两个单元格并通过电子邮件发送不同的范围
- c++ - Make, Endpoints 因预编译器标志而异
- php - 如何从另一个文件访问位于静态函数内的变量?
- vb.net - 如何通过城市代码找到时区?
- java - 在每个代码运行后将数据附加到已使用 java 创建的 excel 中的下两个连续列
- database-design - 与另一个实体有关系的关系属性