I've seen at least five patterns through which you can have some code to run in a worker thread. Most simple:
new Thread(new Runnable() {
public void run() {
//
}
}).start();
We can extend AsyncTask
; we have AsyncTaskLoader
and other Loader
s for data, Service
s and so on.
I assume each one of those has some advantages which I'm not discussing here. What I'm wondering is: which is the most appropriate, concise way to run a simple action?
Defining simple:
- Quite short action, <1s;
- No need to deal with UI elements;
- One line command or such, so it looks inappropriate to extend
AsyncTask
with a new class; - No need to be notified about the success of the task in an
Activity
orFragment
; - No need to display progress;
- Just need to have some kind of
return
.
What I thought at first was:
boolean myReturn;
new Thread(new Runnable() {
public void run() {
myReturn = myExtensiveTask();
}
}).start();
public boolean myExtensiveTask() { ... }
Would this be correct (or even possible)? How should this be done?