1

In a specific part of my code in need to run 3 actions in a a timed space, right now i am using this method:

Handler mHander = new Handler();


public void startChainActions(){

// do first action
      mHandler.postDelayed(new Runnable() {
          @Override
          public void run() {

          //do action 2

                    mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                    //do action 3
                    }
                  }
                }, 1500);

              }
             }
            }, 5000);
           }


public void cleanResources(){

    mHandler.removeCallbacksAndMessages(null);
    mHandler==null
}

When using this method i often see this message on my logcat:

W/art﹕ Suspending all threads took: 12.272ms

which leads me to believe it is slowing down performance. is there a better way to time actions after each other?

Ziv Kesten
  • 1,206
  • 25
  • 41
  • Could you explain us why it is necessary that there is a "sleep" between the execution of the methods? – A.S. Mar 22 '15 at 07:53
  • Have you tried using a CountdownTimer? – cyber_rookie Mar 22 '15 at 08:01
  • The reason is that i need to use AnimationDrawble on a view, using two animations with finite time and have the last one run infinitly, so the first run finishes, the second one replaces it, then that one finishes, and the third infinite one starts, i have not tried using CountdownTimer, is that any better? what is the difference? – Ziv Kesten Mar 22 '15 at 08:28
  • Will an `AnimationSet` help ? http://developer.android.com/reference/android/view/animation/AnimationSet.html – kiranpradeep Mar 22 '15 at 08:32
  • I am using AnimationDrawbles, not Animations... – Ziv Kesten Mar 22 '15 at 08:34

2 Answers2

0

First of all you should understand that in case of Handlers you will execute Runnable in the same thread where handler was created. It means that you will share hardware power with any user<->UI interaction if you run it in a main thread, and there is no guarantee on exact time of execution start.

Answering your question from body - no, there is no slowing down of performance because of 2 postDelayed calls, but could be because of your actions. So your log message is connected with something else.

Update: Answering your question from title: if you'd like to run delayed action, than yes - it's a normal way to do it in Android, especially if you'd to run it in main/UI thread.

Divers
  • 9,531
  • 7
  • 45
  • 88
  • this chain of actions however accrues lots of times in the space, (could accrue up to 2 times a second) is that still not reason enough for performance problem? there is no user interactions on this app, only animations running, most of which are AnimationDrawbles – Ziv Kesten Mar 22 '15 at 08:31
  • Chain of action itself do not cause performance issue, actions do. – Divers Mar 22 '15 at 08:33
  • Thank you, this helps me understand some things, but to answer the title of the question (so that i can accept this as the correct answer), is this a best practice way to commit timed actions in android\java? – Ziv Kesten Mar 22 '15 at 09:03
  • If you'd like to run delayed action, than yes - it's a normal way to do it in Android, especially if you'd to run it in main/UI thread. Hope it answer your question. – Divers Mar 22 '15 at 09:35
0

Can we not use SingleThreadExecutor as below.

ExecutorService animationQueue = Executors.newSingleThreadExecutor();

animationQueue.submit( new Runnable() {
    public void run() {
        // action 1
        Thread.sleep(5000);
    }
});

animationQueue.submit( new Runnable() {
    public void run() {
        // action 2
        Thread.sleep(1500);
    }
});

animationQueue.submit( new Runnable() {
    public void run() {
        // action 3
    }
});
kiranpradeep
  • 10,859
  • 4
  • 50
  • 82
  • I can, however i wish to use handlers for the case where i need to remove all the callbacks from it (and there could be alot on it) and handler gives me the easy option of removeAllCallBacksAndMessages(null). i was just wondering if what i am doing is a hack or a correct way to do this – Ziv Kesten Mar 22 '15 at 09:40
  • @ZivKesten same cleanup for Executors is done with `shutdownNow` method. – kiranpradeep Mar 22 '15 at 09:46
  • Is this better method in your opinion? – Ziv Kesten Mar 22 '15 at 09:59
  • @ZivKesten I won't tell better in terms of performance. But in terms of readability, I will tell far cleaner. – kiranpradeep Mar 22 '15 at 10:04