Are you having trouble using 2 handler.postDelay () at the same time?

2

I would like to know if there is any problem with the App's performance in using 2 handler.postDlay() at the same time, type calling the 2 in functions: inside the OnCreate Td1 (); Td2 ();

public void Td1(){

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {



            handler.postDelayed(this, 5 * 1000);
        }
    }, 5 * 1000);

}

public void Td2(){

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {



            handler.postDelayed(this, 5 * 1000);
        }
    }, 5 * 1000);

}
    
asked by anonymous 31.08.2016 / 00:22

1 answer

3

It will depend on what run() methods do.

Remember that the run() method will be executed in the thread where the handler was created, in this case in UIThread .

>

Also note that since the arguments passed to postDelay are the same and the methods Td1() and Td2() are named after each other, it's practically the same as having only postDelay that runs the contents of each of the run() methods, one after another.

Assume that tasks 1 and 2 are scheduled, one in each postDelay :

public void Td1(){

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {

            tarefa_1();
            handler.postDelayed(this, 5 * 1000);
        }
    }, 5 * 1000);

}

public void Td2(){

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {

            tarefa_2();
            handler.postDelayed(this, 5 * 1000);
        }
    }, 5 * 1000);

}

calling Td1(); and then Td2(); is the same as having only one method (Td3) that schedules both tasks:

public void Td3(){

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {

            tarefa_1();
            tarefa_2();
            handler.postDelayed(this, 5 * 1000);
        }
    }, 5 * 1000);

}

and call only Td3();

    
31.08.2016 / 00:48