我的应用收到消息,并且必须一个接一个地安排多个回复,每个回复都有不同的延迟。有大约2000个具有不同延迟的延迟任务的最佳方法是什么?由于资源有限,似乎Android无法同时运行那么多处理程序。
有什么想法吗?
提前致谢。
代码简化:
class TimedTask extends TimerTask { //runs every 10 seconds and checks for new contacts in queue to reply to (contacts can go up to 500 ones)
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(SendMessagesService.this);
String contacts = sp.getString("contacts", ""); //queued by another Thread
String contacts_array[] = contacts.split(",");
for (String contact : contacts_array) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
String message = sp.getString("message1",""); //there are different messages with different delays, depending on which were already sent
ReceiveContactsAdapter mDbHelper = new ReceiveContactsAdapter(SendMessagesService.this);
mDbHelper.open();
mDbHelper.sendMessages(message, contact);
mDbHelper.close();
}
}, delay); //delay depends on which message will be sent
}
}
答案 0 :(得分:1)
您可以使用thread.sleep(milisec)代替。它会在特定时间内延迟线程。
$(elt).closest('td').not('.savedBlock').remove()