Reputation: 51
how can i correctly stop or interrupt my multiple server's thread? This is my main program's part, here i am starting my multiple server, but i want to implement stop button, cause my programs always stuck, when i am interrupting it. Can i do it right here in main part? which function should i use?
button.setOnClickListener(new View.OnClickListener() {
//OnClickListener oclbutton=new OnClickListener(){
@Override
public void onClick(View v) {
tvOut.setText("Server Started");
Thread thread=new Thread (new MyServer());
thread.start();
}
});
//button.setOnClickListener(oclbutton);
//}
buttonStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//stop server
}
});
Upvotes: 1
Views: 1353
Reputation: 635
A good method to stop a thread is to use a flag to exit from the run() method properly : See this answer
If myServer is waiting on Thread.sleep() you can call myServer.interrupt() to cancel the sleep.
If myServer is blocking on I/O, it is a bit more complicate : Look at this
Upvotes: 0
Reputation: 395
If you want to access the Thread from a different block you should have it declared as a field or a final variable accessible to all the method or class, not on the inner anonymous class, something like this
public void yourMethod() {
final Thread yourThread = null;
button.setOnClickListener(new View.OnClickListener() {
//OnClickListener oclbutton=new OnClickListener(){
@Override
public void onClick(View v) {
tvOut.setText("Server Started");
yourThread=new Thread (new MyServer());
thread.start();
}
});
buttonStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//stop server
yourThread.stop();
}
});
}
now, note that Thread.stop() method has been deprecated, so instead of making that call you could do something on your MyServer class like:
public class MyServer implements Runnable {
private boolean stopExecution = false;
public void setStopExecution(boolean stopExecution) {
this.stopExecution = stopExecution;
}
public void run() {
// as it is a server I assume you're doing a never ending loop
// now just make use of the control variable
while (!stopExecution) {
// do your stuff here...
}
}
}
and on your code what you would do is:
public void yourMethod() { MyServer server = new MyServer();
button.setOnClickListener(new View.OnClickListener() {
//OnClickListener oclbutton=new OnClickListener(){
@Override
public void onClick(View v) {
tvOut.setText("Server Started");
Thread yourThread=new Thread (server);
thread.start();
}
});
buttonStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//stop server
server.setStopExecution(true);
}
});
}
this last way is the right way of doing so to avoid issues with monitors. Also take a look to Executor class to manage your threads.
Upvotes: 0
Reputation: 115378
First, you have to declare your thread variable outside onClick
to be able to access it from the second listener:
final Thread thread=new Thread (new MyServer());
Please pay attention on final
modifier: you are using this variable in anonymous inner classes that can access only final variables of outer method.
Now your first onClick()
will just start the thread.
public void onClick(View v) {
tvOut.setText("Server Started");
thread.start();
}
Now, how to stop the thread?
You can call thread.stop()
. But its method is deprecated for various reasons that beyond of this discussion. You can find reasons in internet.
So, how to stop the thread? It depends on what is it doing. You can either check isInterrupted()
into thread and in this case call thread.interrupt()
from your second listener. Other way is to use boolean flag and wait() - notify()
pair.
There are a lot of tutorials about threads. Read some of them. If something is still unclear come here again and ask more concrete questions. Good luck and enjoy.
Upvotes: 1