我在这里面临一种情况,当使用AsyncTask通过TCP套接字发送消息序列时,除了5个请求中的2个之外,其他任何消息都不会通过网络发送。我也使用Wireshark进行数据包捕获,但没有成功。
这是代码(所有这些方法都在Service类中)。 doTask()是使用AsyncTasks通过tcp套接字发送消息的*方法。我什至使用了ThreadPool,但没有成功。
private class SendMessage extends AsyncTask<EASMessageBase, Void, Void> {
@Override
protected Void doInBackground(EASMessageBase... easMessageBases) {
EASMessageBase msg = easMessageBases[0];
try {
//send data over tcp socket output stream
tcpsocketOutputStream.sendPreparedMessage(msg);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
//DIDN'T WORK EXCEPT FIRST 2 REQUESTS
//without wait statement
public void doTask() {
new SendMessage().execute(message1);
new SendMessage().execute(message2);
new SendMessage().execute(message3);
new SendMessage().execute(message4);
new SendMessage().execute(message5);
}
//WORKED
//using wait statement
public void doTask() {
new SendMessage().execute(message1);
sleep();
new SendMessage().execute(message2);
sleep();
new SendMessage().execute(message3);
sleep();
new SendMessage().execute(message4);
sleep();
new SendMessage().execute(message5);
}
private void sleep() {
try {
Thread.currentThread().sleep(500);
}
catch(Exception e){
}
}
//DIDN'T WORK EXCEPT FOR FIRST 2 REQUESTS
//using handlers without timeout
public void doTask() {
Handler handler = new Handler(Looper.getMainLooper());
//message 1
handler.post(new Runnable() {
@Override
public void run() {
new SendMessage().execute(message1);
}
});
//message 2
handler.post(new Runnable() {
@Override
public void run() {
new SendMessage().execute(message2);
}
});
//message 3
//message 4
//message 5
}
//THIS METHOD WORKED
/*using handlers with timeout (used same timeout, and incremental timeout for each message as well)
1. used same timeout for all messages (e.g. 200)
2. used incremental timeouts for all messages (e.g. 200 for message1, 300 for message2, etc)
*/
public void doTask() {
Handler handler = new Handler(Looper.getMainLooper());
//message1
handler.postDelayed(new Runnable() {
@Override
public void run() {
new SendMessage().execute(message1);
}
}, 200);
//message2
//message3
//message4
//message5
}
答案 0 :(得分:0)
在“不起作用的示例”中,您创建5个不同的AsyncTask,每个均在tcpsocketOutputStream上发送一条消息。这些任务中的每一个异步或多或少地同时执行(取决于可用的CPU内核数)。我假设tcpsocketOutputStream如果尚未完成发送前一个消息,则不适合发出sendPreparedMessage(msg)。
如何只创建一个AsyncTask并在一次调用中提供所有5条消息?
new SendMessage().execute(message1,message2,message3,message4,message5);
然后在doInBackground方法中,在一个循环中发送接收到的5条消息。
for (EASMessageBase msg : easMessageBases)
{
// send the msg
}
或者您的每个异步任务都必须为其自己打开一个套接字连接。