在使用synchronized时,当一个线程得到一个对象锁后,再次请求此对象锁时是可以再次得到该对象锁的.这也证明在一个synchronized方法/块的内部调用本类的其他synchronized方法/块时,是永远可以得到锁的.
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(); } }
运行结果:
可重入锁的概念:
自己可以再次获取自己的内部锁.比如一个线程获取了某个对象的锁,此时这个对象的锁还没有释放,当这个线程想要获取这个对象的锁的时候还是可以获取的,如果不可以锁重入的话,就会造成死锁.
注:可重入锁也适用于父子类继承的环境中.
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(); } }
运行结果:
当存在父子类继承时,子类是完全可以通过可重入锁调用父类的同步方法的.
来源:oschina
链接:https://my.oschina.net/u/2310587/blog/743597