title: Why class cast fails in run method even if checked instanceof, outside run
body: <p>I have the following piece of code:</p>
  
  <pre><code>private void method(final Object obj){
    if (obj instanceof Processor){// assume Processor is some class
      ExecutorService ex = Executors.newSingleThreadExecutor();
  ex.execute(new Runnable() {
  
          @Override
          public void run() {
              process((Processor) obj);//get ClassCastException here
  
          }
      });
  }
  
  private void process(final Processor a){
    //do something
  }
  </code></pre>
  
  <p>My quesion is, even though I check if the obj is an instance of <code>Processor</code> class, and then only I cast it to <code>Processor</code> inside <code>run</code>() method, still I get <code>ClassCastException</code>  at the line where I'm invoking <code>process</code>() method, why?</p>