一个同步方法可以调用另外一个同步方法,一个线程已经拥有某个对象的锁,再次申请的时候仍然会得到该对象的锁也就是说synchronized获得的锁是可重入的

    1. public class T {
    2. synchronized void m() {
    3. System.out.println("m start");
    4. try {
    5. TimeUnit.SECONDS.sleep(2);
    6. } catch (InterruptedException e) {
    7. e.printStackTrace();
    8. }
    9. System.out.println("m end");
    10. }
    11. synchronized void n() {
    12. System.out.println("n start");
    13. try {
    14. TimeUnit.SECONDS.sleep(2);
    15. } catch (InterruptedException e) {
    16. e.printStackTrace();
    17. }
    18. m();
    19. System.out.println("n end");
    20. }
    21. public static void main(String[] args) {
    22. new T().n();
    23. }
    24. }