Hello Poche,
let me give you an actual example.
Imagine you have display a ProgressDialog:
Using java Syntax Highlighting
this.myProgressDialog = ProgressDialog.show(this, "title", "message", true);
Parsed in 0.034 seconds, using
GeSHi 1.0.8.4
Then you do some heavy work, which should not be done in the UI Thread. And in the end of that task you want to dismiss the Dialog:
Using java Syntax Highlighting
new Thread() {
public void run() {
try{
// Do some heavy (fake) work
sleep(5000);
} catch (Exception e) { }
// Dismiss the Dialog
myProgressDialog.dismiss(); // <-- this is not possible
}
}.start();
Parsed in 0.037 seconds, using
GeSHi 1.0.8.4
That is not possible, because UI-Changes can only be done by the UI Thread!
You need to do run that line containing .dismiss on the UI-Thread, like this:
Using java Syntax Highlighting
MyActivity.this.runOnUIThread(new Runnable(){
@Override
public void run() {
myProgressDialog.dismiss(); // this is possible,
}
});
Parsed in 0.036 seconds, using
GeSHi 1.0.8.4
Remember: A class does not contain a thread!
But: The code of a class (object) gets executed by a thread (maybe many at the same time)!
So within the Runnable we could the new Thread go into another class(object) which could then call a function of class we are doing all this in and then dismiss the dialog.
Understanding can be a bit tricky.
Regards,
plusminus