问题描述
- 一个比较深的问题 ,安卓线程 突然挂住了 是不是虚拟机bug
-
以下是一段程序 用的MQTT发数据 中间用了fifo
public void publish( String topic, String message, int qos, boolean retained , Context context)
{Log.e("ZTMQTT.java 632行", "调用到发送函数," + "发送的主题是"+topic);
if(msg_array_block_queue != null)
{
ZTMQTT_msg aZtmqtt_msg=new ZTMQTT_msg();
aZtmqtt_msg.topicString=topic;
aZtmqtt_msg.Msg=message;
aZtmqtt_msg.Quality_of_SerVer=qos;
msg_array_block_queue.offer(aZtmqtt_msg);
Log.e("ZTMQTT.java 682行", "消息已放入fifo" );
}else
{
Log.e("ZTMQTT.java 683行", "消息队列msg_array_block_queue是空 没法发送" );
}
}发送点 public class msgQueueSendThread extends Thread
{
ZTMQTT_msg aZtmqtt_msg=null;@Override // 廖杰增加 public void run() { while (is_a_msgQueueSendThread_start) { try { aZtmqtt_msg=msg_array_block_queue.take();//得到要发送的消息 Log.i("ZTMQTT.java", "771行" + "fifo发送线程离开了阻塞状态"); if ( client!=null&&client.isConnected() == true&&aZtmqtt_msg!=null) { client.publish( aZtmqtt_msg.topicString, aZtmqtt_msg.Msg.getBytes(), MQTT_QUALITY_OF_only_once, false );//这样子发送速度最快了 Log.i("ZTMQTT.java", "771行" + "消息发送成功,主题"+aZtmqtt_msg.topicString+",消息"+aZtmqtt_msg.Msg+"fifo中还剩"+msg_array_block_queue.size()+"条没法"); } else { Log.i("ZTMQTT.java", "776行,线程msgQueueSendThread,id=" + Thread.currentThread().getId() + "客户端未连接消息发送失败"); } Thread.sleep(interval_of_publish_msg);//发送完一个睡觉100ms } catch(Exception e) { Log.i("ZTMQTT.java", "781行,线程msgQueueSendThread,id=" + Thread.currentThread().getId() + "消息发送,遇到异常"); e.printStackTrace(); } } if (msg_array_block_queue != null) { msg_array_block_queue.clear(); //msg_array_block_queue = null; } Log.i("ZTMQTT.java", "线程msgQueueSendThread,id=" + Thread.currentThread().getId() + "被销毁"); }
解决方案
线程挂掉的因素有很多呢,希望你能把出错的问题贴出来,大家一起解决吧,希望能帮到你
时间: 2025-01-20 11:56:34