0
我試圖同時運行線程。我遵循Android開發人員網站上的服務指南(http://developer.android.com/guide/topics/fundamentals/services.html)。在服務中運行多個線程
我稍微修改了代碼,以便不再等待5秒,而是每隔一秒記錄一條消息。
輸出是: MSG1 MSG1 MSG1 MSG1 MSG1
的問題是,我無法弄清楚如何在同一時間運行兩個這些線程,使得輸出是: MSG1 MSG2 MSG1 MSG2 ....
我不斷收到MSG1 MSG1 MSG1 MSG1 MSG1 MSG 2 MSG2 MSG 2 ...
這裏是我的onStartCommand()的代碼:
@Override
public int onStartCommand(Intent intent, int flags, final int startId) {
Log.v("testService","onStartCommand()");
new Thread() {
// This method is called when the thread runs
public void run() {
Message msg = mHandler.obtainMessage();
msg.arg1 = startId;
mHandler.sendMessage(msg);
}
}.start();
return START_STICKY;
}
不應該爲每個請求創建一個新線程,以便以前的請求不是阻止嗎?