title: Behavior of finally block if thread interrupted
body: <p>I am learning threading in java. As per the <a href="http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html" rel="nofollow">description of finally block in an Oracle tutorial</a>: </p>
  
  <blockquote>
    <p>Note: If the JVM exits while the try or catch code is being executed, then the finally block may not execute. <strong>Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.</strong> </p>
  </blockquote>
  
  <p>So I tried to interrupt a thread in try catch block and check whether finally is executed in following class. But as per the output of the program, finally block is executed. Can someone explain what happened?</p>
  
  <pre><code>package com.lock;
  
  import java.util.concurrent.locks.Lock;
  import java.util.concurrent.locks.ReentrantLock;
  
  
   public class TestLock{
       public static void main(String[] args) throws InterruptedException {
            MyThread lockT= new MyThread();
           Thread t= new Thread(lockT); 
           t.setName("TempThread1"); 
           t.start();
           Thread.sleep(1000); 
           t.interrupt();
      }
   }
  
   class MyThread implements Runnable {
          Lock lock;
          public MyThread() {
              lock= new ReentrantLock();
          }
          @Override
          public void run() { 
              try {
                  if(lock.tryLock()){
                           Thread.sleep(5000);
                       while (!Thread.currentThread().isInterrupted()) { 
                              System.out.println("My thread name is   "+ Thread.currentThread().getName());        
                       }                  
                  }
              } catch (Exception e) {
                  e.printStackTrace();                
              }finally{
                  System.out.println("finally ");
                  lock.unlock();
              }           
  
          }
  
  
      }
  </code></pre>