关于JAVA并发库编程的复习(一):synchronized锁重入
发布日期:2021-11-13 10:21:41 浏览次数:11 分类:技术文章

本文共 696 字,大约阅读时间需要 2 分钟。

复习第一节: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...

可见虽然用的是同一把锁,但是在方法里还是可以调用。。。

转载地址:https://blog.csdn.net/Lee_Ho_/article/details/78043312 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:并发编程复习(二):volatile关键字使用
下一篇:关于框架(spring)中ajax请求报406错误的情况

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月09日 13时11分59秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章