public classDemo01{
public synchronized void m1(){
System.out.println(Thread.currentThread().getName()+" come in m1..");
m2();
System.out.println(Thread.currentThread().getName()+" end m1...");}
public synchronized void m2(){
System.out.println(Thread.currentThread().getName()+" come in m2..");
m3();
System.out.println(Thread.currentThread().getName()+" end m2...");}
public synchronized void m3(){
System.out.println(Thread.currentThread().getName()+" end m3...");}
public static void main(String[] args){
Demo01 demo01 = new Demo01();
new Thread(()->{
demo01.m1();},"t1").start();}
private static void reEntryLockMethod(){
final Object o = new Object();
new Thread(()->{// synchronized是可重入锁
synchronized (o){
System.out.println(Thread.currentThread().getName()+" 一层");
synchronized (o){
System.out.println(Thread.currentThread().getName()+" 二层");
synchronized (o){
System.out.println(Thread.currentThread().getName()+" 三层");}}}},"t1").start();}}
public classDemo01{// 显式锁
static Lock lock = new ReentrantLock();
public static void main(String[] args){
new Thread(()->{
lock.lock();try{
lock.lock();
System.out.println(Thread.currentThread().getName());} catch (Exception e){
throw new RuntimeException(e);}finally{
lock.unlock();}
lock.unlock();},"t1").start();
new Thread(()->{
lock.lock();
System.out.println(Thread.currentThread().getName());
lock.unlock();},"t2").start();}}