假如你现在还在为自己的技术担忧,假如你现在想提升自己的工资,假如你想在职场上获得更多的话语权,假如你想顺利的度过35岁这个魔咒,假如你想体验BAT的工作环境,那么现在请我们一起开启提升技术之旅吧,详情请点击
一,首先我们来看一下什么是重入
1 public class Demo1 { 2 3 4 public synchronized void a () { 5 System.out.println("a"); 6 b(); 7 8 try { 9 Thread.sleep(1000);10 } catch (InterruptedException e) {11 e.printStackTrace();12 }13 }14 15 public synchronized void b() {16 System.out.println("b");17 18 try {19 Thread.sleep(1000);20 } catch (InterruptedException e) {21 // TODO Auto-generated catch block22 e.printStackTrace();23 }24 25 }26 27 public static void main(String[] args) {28 final Demo1 d1= new Demo1();29 30 new Thread(new Runnable() {31 32 @Override33 public void run() {34 d1.a();35 }36 }).start();37 38 }39 40 }
a
b从上面的我们可以看出在a里面调用b的时候,按照正常的逻辑会出现死锁的,但是这个时候a,b都输出了,这个就是重入,意思就是拥有同一把锁的时候可以从一个锁调用另一个锁的数据。注意一定是同一把锁,synchronized就是同步锁。
不带重入锁的自定义的lock
1 package com.suning.srcmof.controller.api.notice; 2 3 import java.util.concurrent.TimeUnit; 4 import java.util.concurrent.locks.Condition; 5 import java.util.concurrent.locks.Lock; 6 7 public class MyLock implements Lock { 8 9 private boolean isLocked = false;10 11 12 @Override13 public synchronized void lock() {14 System.out.println("一"+Thread.currentThread().getName());15 while (isLocked )16 try {17 System.out.println("一.一"+Thread.currentThread().getName()+"等待了");18 wait();//当第一个线程执行完之后其它几个线程都在这里等待19 System.out.println("一.二"+Thread.currentThread().getName()+"继续执行");20 } catch (InterruptedException e) {21 e.printStackTrace();22 }23 isLocked = true;24 System.out.println("二"+Thread.currentThread().getName());25 }26 27 @Override28 public synchronized void unlock() {29 System.out.println("三"+Thread.currentThread().getName());30 notify();31 System.out.println("四"+Thread.currentThread().getName()+"唤醒了");32 isLocked = false;33 System.out.println("五"+Thread.currentThread().getName());34 System.out.println("六"+isLocked);35 System.err.println("-----------------------------------------");36 }37 38 @Override39 public void lockInterruptibly() throws InterruptedException {40 41 }42 43 @Override44 public boolean tryLock() {45 return false;46 }47 48 @Override49 public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {50 return false;51 }52 53 54 55 @Override56 public Condition newCondition() {57 return null;58 }59 60 }
1 public class Demo { 2 3 private MyLock lock = new MyLock();//同一把锁 4 5 public void a() { 6 lock.lock(); 7 System.out.println("a"); 8 b(); 9 lock.unlock();10 }11 12 public void b() {13 lock.lock();14 System.out.println("b");15 c();16 lock.unlock();17 }18 19 public void c() {20 lock.lock();21 System.out.println("c");22 lock.unlock();23 }24 25 public static void main(String[] args) {26 final Demo d = new Demo();27 28 new Thread(new Runnable() {29 30 @Override31 public void run() {32 d.a();33 }34 }).start();35 36 /*new Thread(new Runnable() {37 38 @Override39 public void run() {40 d.a();41 }42 }).start();43 44 45 new Thread(new Runnable() {46 47 @Override48 public void run() {49 d.a();50 }51 }).start();52 53 54 new Thread(new Runnable() {55 56 @Override57 public void run() {58 d.a();59 }60 }).start();*/61 }62 63 }
自己的理解:当a方法走完lock的方法的时候,这个时候isLocked是true了,所以调用b的时候就会进入等待的方法题中。
自定义的重入锁
1 package com.suning.srcmof.controller.api.notice; 2 3 import java.util.concurrent.TimeUnit; 4 import java.util.concurrent.locks.Condition; 5 import java.util.concurrent.locks.Lock; 6 7 public class MyLock1 implements Lock { 8 9 private boolean isLocked = false;10 11 private Thread lockBy = null;12 13 private int lockCount = 0;14 15 @Override16 public synchronized void lock() {17 try {18 Thread.sleep(300);19 } catch (InterruptedException e) {20 e.printStackTrace();21 }22 Thread currentThread = Thread.currentThread(); // Thread-023 System.out.println("一:"+Thread.currentThread().getName()+" --isLocked:"+isLocked+" --lockBy:"+lockBy+" --lockCount:"+lockCount+" --currentThread:"+currentThread);24 while (isLocked && currentThread != lockBy)25 try {26 wait();27 } catch (InterruptedException e) {28 e.printStackTrace();29 }30 isLocked = true;31 lockBy = currentThread;32 lockCount ++; // 1 233 System.out.println("二:"+Thread.currentThread().getName()+" --isLocked:"+isLocked+" --lockBy:"+lockBy+" --lockCount:"+lockCount+" --currentThread:"+currentThread);34 }35 36 @Override37 public synchronized void unlock() {38 try {39 Thread.sleep(300);40 } catch (InterruptedException e) {41 e.printStackTrace();42 }43 if(lockBy == Thread.currentThread()) {44 lockCount --; // 1 045 if(lockCount == 0) {46 notify();47 isLocked = false;48 System.out.println("三:"+Thread.currentThread().getName()+" --isLocked:"+isLocked+" --lockBy:"+lockBy+" --lockCount:"+lockCount+" --currentThread:"+Thread.currentThread());49 }50 System.out.println("四:"+Thread.currentThread().getName()+" --isLocked:"+isLocked+" --lockBy:"+lockBy+" --lockCount:"+lockCount+" --currentThread:"+Thread.currentThread());51 }52 System.out.println("五:"+Thread.currentThread().getName()+" --isLocked:"+isLocked+" --lockBy:"+lockBy+" --lockCount:"+lockCount+" --currentThread:"+Thread.currentThread());53 }54 @Override55 public void lockInterruptibly() throws InterruptedException {56 57 }58 59 @Override60 public boolean tryLock() {61 return false;62 }63 64 @Override65 public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {66 return false;67 }68 69 70 71 @Override72 public Condition newCondition() {73 // TODO Auto-generated method stub74 return null;75 }76 77 }
1 public class Demo { 2 3 private MyLock lock = new MyLock();//同一把锁 4 5 public void a() { 6 lock.lock(); 7 System.out.println("a"); 8 b(); 9 lock.unlock();10 }11 12 public void b() {13 lock.lock();14 System.out.println("b");15 c();16 lock.unlock();17 }18 19 public void c() {20 lock.lock();21 System.out.println("c");22 lock.unlock();23 }24 25 public static void main(String[] args) {26 final Demo d = new Demo();27 28 new Thread(new Runnable() {29 30 @Override31 public void run() {32 d.a();33 }34 }).start();35 36 /*new Thread(new Runnable() {37 38 @Override39 public void run() {40 d.a();41 }42 }).start();43 44 45 new Thread(new Runnable() {46 47 @Override48 public void run() {49 d.a();50 }51 }).start();52 53 54 new Thread(new Runnable() {55 56 @Override57 public void run() {58 d.a();59 }60 }).start();*/61 }62 63 }
1 一:Thread-0 --isLocked:false --lockBy:null --lockCount:0 --currentThread:Thread[Thread-0,5,main] 2 二:Thread-0 --isLocked:true --lockBy:Thread[Thread-0,5,main] --lockCount:1 --currentThread:Thread[Thread-0,5,main] 3 a 4 一:Thread-3 --isLocked:true --lockBy:Thread[Thread-0,5,main] --lockCount:1 --currentThread:Thread[Thread-3,5,main] 5 一:Thread-2 --isLocked:true --lockBy:Thread[Thread-0,5,main] --lockCount:1 --currentThread:Thread[Thread-2,5,main] 6 一:Thread-1 --isLocked:true --lockBy:Thread[Thread-0,5,main] --lockCount:1 --currentThread:Thread[Thread-1,5,main] 7 一:Thread-0 --isLocked:true --lockBy:Thread[Thread-0,5,main] --lockCount:1 --currentThread:Thread[Thread-0,5,main] 8 二:Thread-0 --isLocked:true --lockBy:Thread[Thread-0,5,main] --lockCount:2 --currentThread:Thread[Thread-0,5,main] 9 b10 一:Thread-0 --isLocked:true --lockBy:Thread[Thread-0,5,main] --lockCount:2 --currentThread:Thread[Thread-0,5,main]11 二:Thread-0 --isLocked:true --lockBy:Thread[Thread-0,5,main] --lockCount:3 --currentThread:Thread[Thread-0,5,main]12 c13 四:Thread-0 --isLocked:true --lockBy:Thread[Thread-0,5,main] --lockCount:2 --currentThread:Thread[Thread-0,5,main]14 五:Thread-0 --isLocked:true --lockBy:Thread[Thread-0,5,main] --lockCount:2 --currentThread:Thread[Thread-0,5,main]15 四:Thread-0 --isLocked:true --lockBy:Thread[Thread-0,5,main] --lockCount:1 --currentThread:Thread[Thread-0,5,main]16 五:Thread-0 --isLocked:true --lockBy:Thread[Thread-0,5,main] --lockCount:1 --currentThread:Thread[Thread-0,5,main]17 三:Thread-0 --isLocked:false --lockBy:Thread[Thread-0,5,main] --lockCount:0 --currentThread:Thread[Thread-0,5,main]18 四:Thread-0 --isLocked:false --lockBy:Thread[Thread-0,5,main] --lockCount:0 --currentThread:Thread[Thread-0,5,main]19 五:Thread-0 --isLocked:false --lockBy:Thread[Thread-0,5,main] --lockCount:0 --currentThread:Thread[Thread-0,5,main]20 二:Thread-3 --isLocked:true --lockBy:Thread[Thread-3,5,main] --lockCount:1 --currentThread:Thread[Thread-3,5,main]21 a22 一:Thread-3 --isLocked:true --lockBy:Thread[Thread-3,5,main] --lockCount:1 --currentThread:Thread[Thread-3,5,main]23 二:Thread-3 --isLocked:true --lockBy:Thread[Thread-3,5,main] --lockCount:2 --currentThread:Thread[Thread-3,5,main]24 b25 一:Thread-3 --isLocked:true --lockBy:Thread[Thread-3,5,main] --lockCount:2 --currentThread:Thread[Thread-3,5,main]26 二:Thread-3 --isLocked:true --lockBy:Thread[Thread-3,5,main] --lockCount:3 --currentThread:Thread[Thread-3,5,main]27 c28 四:Thread-3 --isLocked:true --lockBy:Thread[Thread-3,5,main] --lockCount:2 --currentThread:Thread[Thread-3,5,main]29 五:Thread-3 --isLocked:true --lockBy:Thread[Thread-3,5,main] --lockCount:2 --currentThread:Thread[Thread-3,5,main]30 四:Thread-3 --isLocked:true --lockBy:Thread[Thread-3,5,main] --lockCount:1 --currentThread:Thread[Thread-3,5,main]31 五:Thread-3 --isLocked:true --lockBy:Thread[Thread-3,5,main] --lockCount:1 --currentThread:Thread[Thread-3,5,main]32 三:Thread-3 --isLocked:false --lockBy:Thread[Thread-3,5,main] --lockCount:0 --currentThread:Thread[Thread-3,5,main]33 四:Thread-3 --isLocked:false --lockBy:Thread[Thread-3,5,main] --lockCount:0 --currentThread:Thread[Thread-3,5,main]34 五:Thread-3 --isLocked:false --lockBy:Thread[Thread-3,5,main] --lockCount:0 --currentThread:Thread[Thread-3,5,main]35 二:Thread-2 --isLocked:true --lockBy:Thread[Thread-2,5,main] --lockCount:1 --currentThread:Thread[Thread-2,5,main]36 a37 一:Thread-2 --isLocked:true --lockBy:Thread[Thread-2,5,main] --lockCount:1 --currentThread:Thread[Thread-2,5,main]38 二:Thread-2 --isLocked:true --lockBy:Thread[Thread-2,5,main] --lockCount:2 --currentThread:Thread[Thread-2,5,main]39 b40 一:Thread-2 --isLocked:true --lockBy:Thread[Thread-2,5,main] --lockCount:2 --currentThread:Thread[Thread-2,5,main]41 二:Thread-2 --isLocked:true --lockBy:Thread[Thread-2,5,main] --lockCount:3 --currentThread:Thread[Thread-2,5,main]42 c43 四:Thread-2 --isLocked:true --lockBy:Thread[Thread-2,5,main] --lockCount:2 --currentThread:Thread[Thread-2,5,main]44 五:Thread-2 --isLocked:true --lockBy:Thread[Thread-2,5,main] --lockCount:2 --currentThread:Thread[Thread-2,5,main]45 四:Thread-2 --isLocked:true --lockBy:Thread[Thread-2,5,main] --lockCount:1 --currentThread:Thread[Thread-2,5,main]46 五:Thread-2 --isLocked:true --lockBy:Thread[Thread-2,5,main] --lockCount:1 --currentThread:Thread[Thread-2,5,main]47 三:Thread-2 --isLocked:false --lockBy:Thread[Thread-2,5,main] --lockCount:0 --currentThread:Thread[Thread-2,5,main]48 四:Thread-2 --isLocked:false --lockBy:Thread[Thread-2,5,main] --lockCount:0 --currentThread:Thread[Thread-2,5,main]49 五:Thread-2 --isLocked:false --lockBy:Thread[Thread-2,5,main] --lockCount:0 --currentThread:Thread[Thread-2,5,main]50 二:Thread-1 --isLocked:true --lockBy:Thread[Thread-1,5,main] --lockCount:1 --currentThread:Thread[Thread-1,5,main]51 a52 一:Thread-1 --isLocked:true --lockBy:Thread[Thread-1,5,main] --lockCount:1 --currentThread:Thread[Thread-1,5,main]53 二:Thread-1 --isLocked:true --lockBy:Thread[Thread-1,5,main] --lockCount:2 --currentThread:Thread[Thread-1,5,main]54 b55 一:Thread-1 --isLocked:true --lockBy:Thread[Thread-1,5,main] --lockCount:2 --currentThread:Thread[Thread-1,5,main]56 二:Thread-1 --isLocked:true --lockBy:Thread[Thread-1,5,main] --lockCount:3 --currentThread:Thread[Thread-1,5,main]57 c58 四:Thread-1 --isLocked:true --lockBy:Thread[Thread-1,5,main] --lockCount:2 --currentThread:Thread[Thread-1,5,main]59 五:Thread-1 --isLocked:true --lockBy:Thread[Thread-1,5,main] --lockCount:2 --currentThread:Thread[Thread-1,5,main]60 四:Thread-1 --isLocked:true --lockBy:Thread[Thread-1,5,main] --lockCount:1 --currentThread:Thread[Thread-1,5,main]61 五:Thread-1 --isLocked:true --lockBy:Thread[Thread-1,5,main] --lockCount:1 --currentThread:Thread[Thread-1,5,main]62 三:Thread-1 --isLocked:false --lockBy:Thread[Thread-1,5,main] --lockCount:0 --currentThread:Thread[Thread-1,5,main]63 四:Thread-1 --isLocked:false --lockBy:Thread[Thread-1,5,main] --lockCount:0 --currentThread:Thread[Thread-1,5,main]64 五:Thread-1 --isLocked:false --lockBy:Thread[Thread-1,5,main] --lockCount:0 --currentThread:Thread[Thread-1,5,main]
假如你现在还在为自己的技术担忧,假如你现在想提升自己的工资,假如你想在职场上获得更多的话语权,假如你想顺利的度过35岁这个魔咒,假如你想体验BAT的工作环境,那么现在请我们一起开启提升技术之旅吧,详情请点击
这个时候就实现了一个重入锁,原理其实和差不多,只不过加了一个数量和是否是同一个锁的判断。