am trying to pause a thread and then resume it but when i do the pause it freeze the application. i try several things but with no luck.
on my main activity am calling the thread and have the button that will pause the thread and a second runnable class it start running and draw on my plot which include and the pause function. the code that i implement is the follow
Main activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDoctorDynamicXYDatasource= new DoctorDynamicXYDatasource(this, mHandler);
findViewById(R.id.Pause).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
synchronized(pp){
mDoctorDynamicXYDatasource.Pause();
Log.i("File", "Pause button ");
}
}
});
findViewById(R.id.Start).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
draw();
}
});
public void draw(){
///some code
pp= new Thread(data);
pp.start();
}
and on the **DoctorDynamicXYDatasource class** am doing the follow
public class DoctorDynamicXYDatasource extends Activity implements Runnable {
public void run() {
//loading and draw on the plot
}
public void Pause() {
synchronized (Thread.currentThread()) {
Log.i("File","pause");
try {
Thread.currentThread().wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
how can i pause the thread and then start it again. when i pause the thread it lock instead only the draw plot all the screen and the button without be able to make a selection
EDIT
i add the code that you tell me and from the main activity with the pause button am calling the pause fuinction from my DoctorDynamicXYDatasource class but it’s not synchronized in order to wait the thread. am also not allow to call the pause function from other class?
mDoctorDynamicXYDatasource= new DoctorDynamicXYDatasource(this, mHandler);
findViewById(R.id.Pause).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("File", "Pause button pressed");
mDoctorDynamicXYDatasource.pause();
}
});
You are calling pause on the UI thread, which indeed results in freezing the application.
you have to pause your specific thread from the inside, like this:
Calling pause will allow the thread to wait at the next iteration. Calling go will allow the thread to exit the wait state immediately.
Afaik, there is no way to force a Thread to pause unless you are in the thread.