looking at several android examples i see a trend where threads are paused using join() method:
EDIT: please assume that the 2 threads are properly defined. edited the calls to join()
public class MainUI extends Activity{
private GraphicsViewThread g;
private BackendDataViewThread b;
void onCreate(){
g = new GraphicsViewThread();
g.start();
b = new BackendDataViewThread();
b.start();
}
.
.
.
void pauseGame(){
try {
g.join();
}catch (InterruptedException e){
e.printStackTrace();
}
try {
b.join();
}catch (InterruptedException e){
e.printStackTrace();
}
GraphicsViewThread = null;
BackendDataViewThread = null;
}
}
- which thread does GraphicsViewThread and BackendDataViewThread join to?
- what is the need for using join()? can i not set the references immediately to null?
I suspect your code doesn’t compile, but regardless – I’ll answer.
This is not just about Android, but plain Java.
Using “join” ensures that you code will wait for the thread to end.
Isn’t this exactly what you want?
In addition, don’t set references of thread to null.
You cannot guarantee exactly when a thread ends, so it will be bad practice to set the thread object to null, prior to its ending.