复习第一节:synchronized锁重入
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /** * 锁重入 * @author lihao * */ public class MyThread3 { public synchronized void method1(){ System.out.println("method1..."); method2(); } public synchronized void method2(){ System.out.println("method2..."); method3(); } public synchronized void method3(){ System.out.println("method3..."); } public static void main(String[] args) throws ParseException { MyThread3 myThread3 = new MyThread3(); Thread thread = new Thread(new Runnable() { @Override public void run() { myThread3.method1(); } }); thread.start(); } } |
method1...
method2...
method3...
可见虽然用的是同一把锁,但是在方法里还是可以调用。。。