I have the following void:
public void load() {
//loading big picture from the Internet
}
And i want it to run in a new Thread.
I can call this procedure like this:
new Thread(new Runnable() {
public void run() {
load();
}
}).start();
or it would be better to modify this void:
public void load() {
new Thread(new Runnable() {
public void run() {
//loading big picture from the Internet
}
}).start();
}
and simply call it:
load();
Or there is no different?
Functionally, they are identical. There are designed details that might make you consider the first way over the other.
You may want the first option if you had a lot of different threads that didn’t their own individual things but also called
load(). In that case, you already had created the thread, so if they calledload()you wouldn’t want/need to create another.The second option is very convenient. You can simply call
load()wherever you want. If, in the future, theload()method changes to a point where it’s not blocking, then you can change it and no further code changes would be needed.Alternatively, consider using AsyncTask for this as previously suggested. It was specifically built for exactly what you’re trying to do.