package reentrantlock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 重入锁 reentrantLock
 */
public class ReenterLock implements Runnable {
    private static ReentrantLock lock = new ReentrantLock();
    private static int i = 0;

    public ReenterLock(ReentrantLock lock) {
        this.lock = lock;
    }

    @Override
    public void run() {
        for (int j = 0; j < 100; j++) {
            lock.lock();
            try {
                i++;
            } finally {
                lock.unlock();
            }
        }
//        System.out.println(Thread.currentThread().getId() + ":" + i);
    }

    public static void main(String[] args) throws InterruptedException {



//        ReenterLock lock = new ReenterLock();
//        Thread thread1 = new Thread(lock);
//        Thread thread2 = new Thread(lock);
//        thread1.start();
//        thread2.start();
//        thread1.join();
//        thread2.join();
//        System.out.println(i);
        ReentrantLock reentrantLock=new ReentrantLock();
        ReenterLock lock1 = new ReenterLock(reentrantLock);
        ReenterLock lock2 = new ReenterLock(reentrantLock);
        Thread thread1 = new Thread(lock1);
        Thread thread2 = new Thread(lock2);
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
        System.out.println(i);
    }
}

@@ -235,16 +235,18 @@
 i = 0;%0A%0A
+//
     publ
@@ -278,24 +278,26 @@
 ock lock) %7B%0A
+//
         this
@@ -310,16 +310,18 @@
 = lock;%0A
+//
     %7D%0A%0A 
@@ -369,39 +369,233 @@
     
-for (int j = 0; j %3C 100; j++) %7B
+System.out.println(Thread.currentThread() + %22-------------start%22);%0A        for (int j = 0; j %3C 100; j++) %7B%0A            System.out.println(Thread.currentThread() + %22-------------start%22 + j);%0A//            lock.lock();%E5%A4%9A%E9%94%81%E4%B8%80%E6%AC%A1%EF%BC%8C %E9%94%81%E7%AD%89%E5%BE%85
 %0A   
@@ -702,32 +702,142 @@
  lock.unlock();%0A
+//                lock.unlock(); %E5%B0%91%E9%87%8A%E6%94%BE%E4%B8%80%E6%AC%A1  Exception in thread %22Thread-0%22 java.lang.IllegalMonitorStateException%0A
             %7D%0A  
@@ -998,19 +998,16 @@
 ion %7B%0A%0A%0A
-%0A//
         
@@ -1040,26 +1040,24 @@
 nterLock();%0A
-//
         Thre
@@ -1079,34 +1079,32 @@
 w Thread(lock);%0A
-//
         Thread t
@@ -1130,18 +1130,16 @@
 (lock);%0A
-//
         
@@ -1147,34 +1147,32 @@
 hread1.start();%0A
-//
         thread2.
@@ -1176,26 +1176,24 @@
 d2.start();%0A
-//
         thre
@@ -1196,34 +1196,32 @@
 thread1.join();%0A
-//
         thread2.
@@ -1224,26 +1224,24 @@
 ad2.join();%0A
-//
         Syst
@@ -1255,24 +1255,26 @@
 println(i);%0A
+//
         Reen
@@ -1296,17 +1296,19 @@
 rantLock
-=
+ = 
 new Reen
@@ -1312,32 +1312,34 @@
 eentrantLock();%0A
+//
         ReenterL
@@ -1374,32 +1374,34 @@
 reentrantLock);%0A
+//
         ReenterL
@@ -1440,24 +1440,26 @@
 trantLock);%0A
+//
         Thre
@@ -1490,16 +1490,18 @@
 lock1);%0A
+//
         
@@ -1536,16 +1536,18 @@
 lock2);%0A
+//
         
@@ -1555,32 +1555,34 @@
 hread1.start();%0A
+//
         thread2.
@@ -1582,32 +1582,34 @@
 hread2.start();%0A
+//
         thread1.
@@ -1608,32 +1608,34 @@
 thread1.join();%0A
+//
         thread2.
@@ -1634,32 +1634,34 @@
 thread2.join();%0A
+//
         System.o

add reentrantLock 重入锁