package reentrantlock;

import java.util.concurrent.locks.ReentrantLock;

public class InterruptLock implements Runnable {
    private static ReentrantLock lock1 = new ReentrantLock();
    private static ReentrantLock lock2 = new ReentrantLock();

    private static int i = 0;

    public InterruptLock(int i) {
        i = i;
    }

    @Override
    public void run() {
        if (i == 1) {
            try {
                System.out.println(Thread.currentThread() + "ready to get lock1");
                lock1.lockInterruptibly();
                System.out.println(Thread.currentThread() + "get lock1");
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            try {
                System.out.println(Thread.currentThread() + "ready to get lock2");
                lock2.lockInterruptibly();
                System.out.println(Thread.currentThread() + "get lock2");
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } else {

            try {
                System.out.println(Thread.currentThread() + "ready to get lock2");
                lock2.lockInterruptibly();
                System.out.println(Thread.currentThread() + "get lock2");
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            try {
                System.out.println(Thread.currentThread() + "ready to get lock1");
                lock1.lockInterruptibly();
                System.out.println(Thread.currentThread() + "get lock1");
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

    public static void main(String[] args) throws InterruptedException {
        InterruptLock interruptLock = new InterruptLock(1);
        Thread thread1 = new Thread(interruptLock);
        InterruptLock interruptLock2 = new InterruptLock(2);
        Thread thread2 = new Thread(interruptLock2);
        thread1.start();
        thread2.start();

        thread1.join();
        thread2.join();

        thread1.interrupt();
    }
}

@@ -378,19 +378,11 @@
     
-if (i == 1)
+try
  %7B%0A 
@@ -384,35 +384,43 @@
 y %7B%0A            
-try
+if (i == 1)
  %7B%0A             
@@ -646,124 +646,8 @@
 0);%0A
-            %7D catch (InterruptedException e) %7B%0A                e.printStackTrace();%0A            %7D%0A%0A            try %7B
 %0A   
@@ -897,125 +897,12 @@
   %7D 
-catch (InterruptedException e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D else %7B%0A%0A            try
+else
  %7B%0A 
@@ -1140,124 +1140,8 @@
 0);%0A
-            %7D catch (InterruptedException e) %7B%0A                e.printStackTrace();%0A            %7D%0A%0A            try %7B
 %0A   
@@ -1386,16 +1386,26 @@
        %7D
+%0A        %7D
  catch (
@@ -1426,16 +1426,17 @@
 eption e
+x
 ) %7B%0A    
@@ -1443,51 +1443,274 @@
     
-        e.printStackTrace();%0A            %7D%0A
+%7D finally %7B%0A            if (lock1.isHeldByCurrentThread()) %7B%0A                lock1.unlock();%0A            %7D%0A%0A            if (lock2.isHeldByCurrentThread()) %7B%0A                lock2.unlock();%0A            %7D%0A%0A            System.out.println(Thread.currentThread()+%22%E7%BA%BF%E7%A8%8B%E9%80%80%E5%87%BA%22);
 %0A   

add reentrantLock 重入锁 中断