Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handler postDelayed and Thread.sleep()

I have a thread.sleep and a handler postDelayed in my code:

handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        Log.e(TAG, "I ran");
        mIsDisconnect = false;
    }
}, DISCONNECT_DELAY);

After the handler code and after the user press the button I have this:

while (mIsDisconnect) {
    try {
        Thread.sleep(DELAY);
    } catch (InterruptedException e) {
        Log.e(TAG, "problem sleeping");
    }
}

If the user wait long enough I can get the "I ran" in my log. But if the user press the button before the delay is up, it seems that the postDelayed never gets a chance to execute. My question is, does the thread.sleep() mess with the handler postDelayed?

Edit: The purpose of this code is that I want to continue the program only after DISCONNECT_DELAY seconds has already passed. So if the user clicks to early, I have to wait for the elapsed time to finish.

like image 498
Jace Avatar asked Sep 10 '13 07:09

Jace


People also ask

What is the difference between handler and Handlerthread?

Threads are generic processing tasks that can do most things, but one thing they cannot do is update the UI. Handlers on the other hand are background threads that allow you to communicate with the UI thread (update the UI).

What is Handler and runnable?

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue.

What is the use of Handlers?

In android Handler is mainly used to update the main thread from background thread or other than main thread. There are two methods are in handler. Post() − it going to post message from background thread to main thread using looper.

What are thread Handlers in Android?

A Handler is a component that can be attached to a thread and then made to perform some action on that thread via simple messages or Runnable tasks. It works in conjunction with another component, Looper , which is in charge of message processing in a particular thread.


1 Answers

I'm assuming your handler is associated with the same thread the other loop is running on. (A Handler is associated with the thread it is created in.)

postDelayed() puts the Runnable in the handler thread's message queue. The message queue is processed when control returns to the thread's Looper.

Thread.sleep() simply blocks the thread. The control does not return to the Looper and messages cannot be processed. Sleeping in the UI thread is almost always wrong.

To accomplish what you're trying to do, remove the sleep and simply use postDelayed() to post a Runnable that changes your app state (like you already do by setting a member variable mIsDisconnect). Then in the onClick() just check the app state (mIsDisconnect flag) whether it is ok to proceed or not.

like image 162
laalto Avatar answered Oct 11 '22 12:10

laalto