title: How to track and implement cancel option for nested Runnable in java
body: <p>I am working on a Java application which uses <code>ZMQ</code> to handle request. My code have some nested <code>Runnable</code> which are as in following structure:</p>
  
  <pre><code>private class SomeRunnable implements Runnable {
      @Override
      public void run() {
          while (!stop) {
              try {
                  createProcess();
              }
              catch (Exception e) {
                  //handled
              }
          }
      }
  }
  
  public void createProcess(){
      ZMsg receivedRequest = receiveReq(); // wait for request
      Runnable workerRunnable = new WorkerRunnable(receivedRequest, this);
      Thread workerThread = new Thread(workerRunnable);
      workerThread.start();
  }
  </code></pre>
  
  <p>So Each time I send a request using <code>Fiddler</code>, it will create new <code>WorkerRunnable</code> and pass the <code>zmq</code> request to it for futher processing.
  This WorkerRunnable is defined as :</p>
  
  <pre><code>private class WorkerRunnable implements Runnable {
      @Override
      public void run() {
              try {
                  // do something with received request
                  switch(type) : 
                  case EXECUTE:
                      startExecution(); // this will internally create one more thread
                      break;
  
                  case STOP:
                      stopExecution();
                      break;                      
              }
              catch (Exception e) {
                  //handled
              }
          }
      }
  }
  </code></pre>
  
  <p>and here is the very simple workflow:</p>
  
  <ol>
  <li><p>I made a request using <code>fiddler</code> to execute something. It will create one WorkerRunnable.</p></li>
  <li><p>In next request I want to stop the <code>WorkerRunnable</code> started in step 1.</p></li>
  </ol>
  
  <p>It may happen that I made multiple request then among them I want to stop any one request. How to track all WorkerRunnable and how to interrupt them if request comes to stop them?
  I tried <code>ExecutorService</code> but didn't found any exact way to use it in this scenario. Please suggest proper way to do that.</p>