title: Java object implements Runnable, how to remove the object from a collection
body: <p>I have a java object that implements Runnable. here is the code.</p>
  
  <pre><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
          }
  }
  
  }
  </code></pre>
  
  <p>here is how i use the Obj.</p>
  
  <pre><code>Obj o = new Obj();
  //q is a collection of Obj
  q.add(o);
  new Thread(o).start();
  </code></pre>
  
  <p>when want to remove o, may I safely do this:</p>
  
  <pre><code>o.setshouldRun(false); //thread will finish
  q.remove(o);
  </code></pre>