I have a java object that implements Runnable. here is the code.
public class Obj implements Runnable {
boolean shouldRun;
private int stuff
public Obj() {
this.setshouldRun(true);
stuff = 0;
}
public synchronized void setshouldRun(boolean shouldRun) {
this.shouldRun = shouldRun;
}
public synchronized boolean getshouldRun() {
return this.shouldRun;
}
@Override
public void run() {
while (shouldRun) {
//do stuff
}
}
}
here is how i use the Obj.
Obj o = new Obj();
//q is a collection of Obj
q.add(o);
new Thread(o).start();
when want to remove o, may I safely do this:
o.setshouldRun(false); //thread will finish
q.remove(o);