在使用synchronized时,当一个线程获得一个对象锁后,再次请求此对象锁时是能够再次获得该对象锁的.这也证实在一个synchronized方法/块的内部调用本类的其余synchronized方法/块时,是永远能够获得锁的.ide
public class ThreadReenter { synchronized public void firstService() { System.out.println("This is the first service!"); secondService(); } synchronized public void secondService() { System.out.println("This is the second service!"); thirdService(); } synchronized public void thirdService() { System.out.println("This is the third service!"); } }
public class FirstThread extends Thread { @Override public void run(){ ThreadReenter threadReenter = new ThreadReenter(); threadReenter.firstService(); } }
public class Run { public static void main(String[] args) { FirstThread firstThread = new FirstThread(); firstThread.start(); } }
运行结果:this
可重入锁的概念:线程
本身能够再次获取本身的内部锁.好比一个线程获取了某个对象的锁,此时这个对象的锁尚未释放,当这个线程想要获取这个对象的锁的时候仍是能够获取的,若是不能够锁重入的话,就会形成死锁.对象
注:可重入锁也适用于父子类继承的环境中.继承
public class ThreadReenter { public int i = 10; synchronized public void ancestor(){ try{ i--; System.out.println("the ancestor i = " + i); Thread.sleep(200); }catch (Exception e){ e.printStackTrace(); } } }
public class SonOfThreadReenter extends ThreadReenter{ synchronized public void Posterity(){ try{ while(i > 0){ i--; System.out.println("the Posterity i = " + i); Thread.sleep(200); this.ancestor(); } }catch (Exception e){ e.printStackTrace(); } } }
public class FirstThread extends Thread { @Override public void run(){ SonOfThreadReenter sonOfThreadReenter = new SonOfThreadReenter(); sonOfThreadReenter.Posterity(); } }
public class Run { public static void main(String[] args) { FirstThread firstThread = new FirstThread(); firstThread.start(); } }
运行结果:同步
当存在父子类继承时,子类是彻底能够经过可重入锁调用父类的同步方法的.it