title: How can I interrupt a synchronized statement in Java?
body: <p>I have two threads that want to synchonize on the same object. Thead <code>A</code> needs to be able to interrupt Thread <code>B</code> if a certain condition has been fullfilled. Here is some pseudo-code of what the two threads do/should do.</p>
  
  <h3>A:</h3>
  
  <pre><code>public void run()
  {
      while(true)
      {
          //Do stuff
          synchronized(shared)
          {
              //Do more stuff
              if(condition)
              {
                  B.interrupt();
              }
          }
      }
  }
  </code></pre>
  
  <h3>B:</h3>
  
  <pre><code>public void run()
  {
      while(true)
      {
          try
          {
              //Do stuff
              synchronized(shared)
              {
              //Do more stuff
              }
          }
          catch(InterruptedException e)
          {
              continue;
          }
      }
  }
  </code></pre>
  
  <p>Here's the situation I can't resolve:</p>
  
  <ul>
  <li>Thread <code>A</code> grabs the shared resource and does some stuff.</li>
  <li>Meanwhile, Thread <code>B</code> reaches the synchronized block, and awaits for <code>A</code> to release its shared resource.</li>
  <li>Thread <code>A</code>, while doing stuff, realized that Thread B should not have the shared resource, and tries to interrupt Thread <code>B</code>. But Thread <code>B</code> has already surpassed the points where an <code>InterruptedException</code> could be thrown.</li>
  </ul>
  
  <p>My question is, is there any way to interrupt a thread while it is waiting to be <code>synchronized</code> on something?</p>