0

I am talking about one thread. For example I have an Activity ui and following methods in it:

/* TOP LEVEL OF EXECUTION LOOPER NOW WORKING */

class MyActivity extends Activity {
void onCreate(Bundle instance) {
 super.onCreate(instance);
 setContentView(R.layout.activity_main);
 doComplicatedStuff();
}

void doComplicatedStuff() {
 // Doing stuf
}

void onClick() {
 // Process event
}

void anyOtherMethod() {
  /* NOT TOP LEVEL OF EXEUCTION, LOOPER NOW CAN'T WORK */
}
}

/* TOP LEVEL OF EXECUTION, LOOPER NOW WORKING */

So my question is, can doComplicatedStuff() be interrupted by execution of onClick() handler (when of course, we had a clicked button) ?

Now I think than onClick() handler can't interrupt doComplicatedStuff() execution until doComplicatedStuff() ends its work. Because on top level of code execution we have an Looper, that accepts next message event and dispatch it to Handler (handler then call onClick() method). In other words, Looper do your work only when there is no any executing method in this thread.

1 Answers1

1

You're correct. The GUI thread will be busy in the onCreate function, and so the onClick method can't be called to interrupt complicatedStuff, even if submitting an item to the looper.

In fact, this sort of thing would only be possible if more than one thread were involved. Even then, if it required a submission to runOnUiThread, it would likely fail as a long running operation is in progress.

I suggest you perform your complicatedStuff routine on a second thread. Long running operations do not belong on the UI thread.

William Morrison
  • 10,953
  • 2
  • 31
  • 48
  • Thank you very much! Yesterday I have asked a question. This question was about runOnUiThread, so I suppose that answer in it question (http://stackoverflow.com/questions/17489551/multiple-asynctasks-onpostexecute-order) is incorrect? – Cyrus Smith Jul 06 '13 at 05:21
  • His answer is correct, and so is mine. The two address separate issues. Mine assumes only one thread is running. His does not, so tasks could be executed asynchronously. However, only one task at a time will update the GUI. When one item is working on the GUI, it is possible this item could interrupt some other async work if you desire. Other tasks, however, could be in any state. Might even already be finished. I would not suggest this approach. – William Morrison Jul 06 '13 at 05:39