3

It may look a very funny and silly question..

I am trying to look around the Background operations with Runnables, Threads, Services and Intent Services in Android application.

So I created an activity and created a simple thread inside the activity like,

public class ExectuableThread implements Runnable{
  @Override
  public void run() {
    Log.e("current-thread", String.valueOf(Looper.getMainLooper().isCurrentThread())); // **Returning true**
    btnDone.setText("will not work");
  }
}

So in the above scenario button text is changing.

Doesn't matter I am calling like:

Thread t = new Thread (new ExectuableThread());
t.run();

OR

Thread t = new Thread (new ExectuableThread());
t.start();

Why My button text is changing if by calling start(); -when a background thread is used?

Now a very funny scenario; if I put a 2 sec delay, like this;

public class ExectuableThread implements Runnable{
  @Override
  public void run() {
    try {
      Thread.sleep(2000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    Log.e("current-thread", String.valueOf(Looper.getMainLooper().isCurrentThread()));
    btnDone.setText("will not work");
  }
}

Then view is not getting updated if I call start(); in run() calling case. It will work.

Difference between start() and run() is clear but question is same why button text is updating if the Thread is in background.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Custadian
  • 845
  • 1
  • 11
  • 37
  • i think this will help you to understand diff between run() and start() methods http://javarevisited.blogspot.in/2012/03/difference-between-start-and-run-method.html#axzz4ip9wGDe1 – santoXme Jun 02 '17 at 06:34
  • @santoXme Thanks. This I understand but will be helpful If you can correct me in my question or answer that. :) – Custadian Jun 02 '17 at 06:58

2 Answers2

2

First of all, your naming isn't too good:

public class ExectuableThread implements Runnable {

would imply that instances of this class are threads, but of course they aren't. So you are adding confusion to the whole issue right there.

My question is Why My button text is changing if by calling start(); Thread runs in the background.

Thing is: when you are not doing things the "right way", especially in multi-thread, all kinds of things can happen.

Meaning: in order to update UI elements in Android, you should be using runOnUiThread. Updating UI elements within other threads might work, or might not work.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Sorry for the naming and 'Executable' spelling; now onwards will keep in mind but I was testing the code in dummy app. runOnUiThread works; I knew runOnUiThread will work but still was curious why this was happening. – Custadian Jun 02 '17 at 06:51
  • I agree that finding out such things is interesting, but that's the thing with multi-threading: when you do it the wrong, all bets are off. All kinds of things can happen. – GhostCat Jun 02 '17 at 07:04
-1

Main difference is that when program calls start() method a new Thread is created and run() method is executed in new Thread while if you call run() method directly no new Thread is created and code inside run() will execute on current Thread.

And second difference is if you call start() method twice then it will throw IllegalStateException

gati sahu
  • 2,576
  • 2
  • 10
  • 16