Android – how to handle multiple runnable objects at the same time

I tried to create multiple task queues and execute them at the same time, but the delay time was different. Basically, at first I had only 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 the code so that there are multiple queues and all queues start executing at the same time, but each queue may have a different updateperiod, depending on the tasks in it. In this way, I can classify tasks into queues and manually control the update speed. How do I implement it?

thank you.

resolvent:

You need to 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 information and perform actions according to the information content:

mWorkerHandler.sendEmptyMessageDelayed(1, 2000);
mWorkerHandler.sendEmptyMessage(2);

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>