JProgrammer
JProgrammer

Reputation: 95

Runnable posts in the handler thread execute one after one

I write the code that I've seen in a tutorial video. In that tutorial the delayed post execute after pass the specified time and didn't care about the pass posts. But in my code the post execute right after the first runnable and can't break that

my code :

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";
TextView txtOutput;

private HandlerThread handlerThread = new HandlerThread("HandlerThread");
private Handler threadHandler ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtOutput = findViewById(R.id.txtOutput);
    handlerThread.start();
    threadHandler = new Handler(handlerThread.getLooper());
}

public void doWork(View view) {
    threadHandler.postDelayed(new ExampleRunnable1(),1000);
    threadHandler.post(new ExampleRunnable2());
}

public void removeMessages(View view) {

}

@Override
protected void onDestroy() {
    super.onDestroy();
    handlerThread.quit();
}

static class ExampleRunnable1 implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            Log.d(TAG, "Runnable1 : " + i);
            SystemClock.sleep(500);
        }
    }
}
static class ExampleRunnable2 implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            Log.d(TAG, "Runnable2 : " + i);
            SystemClock.sleep(500);
        }
    }
}

}

the code give me this :

D/MainActivity: Runnable2 : 0
D/MainActivity: Runnable2 : 1
D/MainActivity: Runnable2 : 2
D/MainActivity: Runnable2 : 3
D/MainActivity: Runnable2 : 4
D/MainActivity: Runnable1 : 0
D/MainActivity: Runnable1 : 1
D/MainActivity: Runnable1 : 2
D/MainActivity: Runnable1 : 3
D/MainActivity: Runnable1 : 4

But i expect that the Runnable1 execute just after Runnable2 : 1 (because of 1000 mSecond wait) but as you can see Runnable1 waits until Runnable2 reach to the end

Upvotes: 0

Views: 639

Answers (1)

varunkr
varunkr

Reputation: 5542

The code is working as expected.

threadHandler.postDelayed(new ExampleRunnable1(),1000);
threadHandler.post(new ExampleRunnable2());

When you do the following, you are adding two jobs to the MessageQueue which are executed sequentially. So only after the first job finished, the second one will start.

The question is which job is executed first?

Both post and postDelayed call sendMessageDelayed, but post uses a delay of 0. sendMessageDelayed states that the message is put in the queue after all pending requests. The reason is that each message is enqueued with the time it was enqueued plus an optional delay. The queue is ordered by this time value. If you enqueue a new message with no delay it will skip (be placed in front of) delayed messages that still have not reached their delivery time.

So the ExampleRunnable2 starts running first. And when it finishes execution, ExampleRunnable1 starts running.

Upvotes: 1

Related Questions