Reputation: 677
I was trying to create multiple queues of tasks and execute them at the same time but with different delay time. Basically, at first I only have one runnable object
private final Runnable myQueue = new Runnable() {
public void run() {
if (service != null && service.isRunning() && service.queueEmpty()) {
queueTasks();
}
// run again in period defined in preferences
new Handler().postDelayed(myQueue,getUpdatePeriod(prefs));
}
};
private void StartWExecute() {new Handler().post(myQueue);}
I want to improve my code so that there will be more than one queue, and all queues start executing at the same time, but each queue may have different updatePeriod depending on the task in it. In this way I can sort tasks into queues and manually control the update speed. How do I achieve this?
Thanks.
Upvotes: 4
Views: 4410
Reputation: 390
You need use a MessageQueue:
1.Declare a Handler:
Handler mWorkerHandler;
2.Create a Looper:
Thread t = new Thread() {
@Override
public void run() {
Looper.prepare();
mWorkerHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Log.d(TAG, "handleMessage - what = " + msg.what);
}
};
Looper.loop();
}
};
t.start();
3.Now you can send any number messages and perform operations according to message content:
mWorkerHandler.sendEmptyMessageDelayed(1, 2000);
mWorkerHandler.sendEmptyMessage(2);
Upvotes: 4