同步锁

Monitor

  • monitor指与synchronized关联的同步资源所关联的锁
  • monitor有一个计数器,初始化为0,如果monitor的计数器为0,则意味着该monitor的lock还没有被获得,某个线程获得之后将立即对该计数器加一,从此该线程就是这个monitor的所有者了
  • 如果一个已经拥有该monitor所有权的线程重入(重新调用该资源),monitor的计数器会再次累加
  • 如果monitor已经被其他线程所拥有,则其他线程尝试获取该monitor的所有权时,会被陷入阻塞状态知道monitor计数器变为0,才能再次尝试获取对monitor的拥有权

使用synchronized需要注意的问题

  1. 与monitor关联的对象不能为空

    private final Object mutex=null;
    public void synMethod(){synchronized(mutex){//TODO}
    }
    
  2. synchronized关键字作用域不应该太大

    public static class Task implements Runnable{public synchronized void run(){//TODO}
    }
    

    ​ 上面的代码对整个线程的执行域也就是run方法都进行了synchronized同步,从而丧失了并发的能力,synchronized关键字应该尽可能的只作用于共享资源的读写作用域

  3. 不同的minitor企图锁相同的方法

    //模拟排队取号
    package online.hengtian.Thread;import java.util.Arrays;
    import java.util.List;
    class Run implements Runnable{private int index=1;private final static int MAX=500;private final static Object obj=new Object();@Overridepublic void run() {synchronized (obj) {while (index <= MAX) {System.out.println(Thread.currentThread() + " 的号码是:" + index++);}}}
    }
    public class TicketWindow {public static void main(String[] args){List<String> windows= Arrays.asList("一号窗口","二号窗口","三号窗口","四号窗口");
    /*****************错误示例开始*********************************/windows.stream().map(t->new Thread(new Run(),t)).forEach(Thread::start);
    /*****************错误示例结束*********************************/windows= Arrays.asList("一号窗口","二号窗口","三号窗口","四号窗口");Runnable r=new Run();windows.stream().map(t->new Thread(r,t)).forEach(Thread::start);}
    }

    ​ 在上面所示的错误示例中,构造了四个Runnable实例,从而让每个线程都拥有了自己的monitor,起不到互斥的作用,在错误实例的下面也给出了正确的做法

  4. 多个锁交叉导致死锁

    private final Object READ=new Object();
    private final Object WRITE=new Object();
    public void read(){synchronized(READ){synchronized(WRITED){//TDDO}}
    }
    public void write(){synchronized(WRITE){synchronized(READ){//TDDO}}
    }
    

This Monitor和Class Monitor的详细介绍

This Monitor

​ synchronized关键字修饰的同一个实例对象的不同方法时,线程争抢的是同一个Monitor的lock,也就是this的Monitor,通过代码二可以验证

//代码一
package online.hengtian.Thread;import java.util.concurrent.TimeUnit;public class MonitorDemo {public synchronized void Method1(){System.out.println("Now here is method1");try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("Method1 Termined");}public synchronized void Method2(){System.out.println("Now here is method2");try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("Method2 Termined");}public static void main(String[] args){MonitorDemo demo=new MonitorDemo();new Thread(demo::Method1,"t1").start();new Thread(demo::Method2,"t2").start();}
}
//代码二
package online.hengtian.Thread;import java.util.concurrent.TimeUnit;public class MonitorDemo {public synchronized void Method1(){System.out.println("Now here is method1");try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("Method1 Termined");}public void Method2(){synchronized (this) {System.out.println("Now here is method2");try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("Method2 Termined");}}public static void main(String[] args){MonitorDemo demo=new MonitorDemo();new Thread(demo::Method1,"t1").start();new Thread(demo::Method2,"t2").start();}
}

​ 代码一是采用的同步方法的方式,代码而是用this的monitor,运行后效果一样,都是运行完方法一之后再运行方法二

Class Monitor

//代码一
package online.hengtian.Thread;import java.util.concurrent.TimeUnit;public class ClassMonitor {public static synchronized void Method1(){System.out.println("Now here is method1");try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("Method1 Termined");}public static synchronized void Method2(){System.out.println("Now here is method2");try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("Method2 Termined");}public static void main(String[] args){new Thread(ClassMonitor::Method1,"t1").start();new Thread(ClassMonitor::Method2,"t2").start();}
}
//代码二
package online.hengtian.Thread;import java.util.concurrent.TimeUnit;public class ClassMonitor {public static synchronized void Method1(){System.out.println("Now here is method1");try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("Method1 Termined");}public static void Method2(){synchronized(ClassMonitor.class){System.out.println("Now here is method2");try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("Method2 Termined");}}public static void main(String[] args){new Thread(ClassMonitor::Method1,"t1").start();new Thread(ClassMonitor::Method2,"t2").start();}
}

​ 代码一和二的运行结果一致,都是先运行方法一之后执行方法二,从而可以说明,在同一个类中的静态方法同步化时,竞争的是class的monitor

SynchronizedMonitor总结相关推荐

最新文章

  1. js 点击闭包_学习Javascript闭包(Closure)
  2. Unreal Engine 4 编码规范
  3. oracle 不等函数,Oracle 不常用函数
  4. POJ 2104 划分树
  5. Java基本语法-----java标识符
  6. select 下拉框的选中项的change事件
  7. Spring配置XML本地提示:点击eclipse属性——选择XML Catalog
  8. 每日算法系列【LeetCode 523】连续的子数组和
  9. php和apache配置 LoadModule php5_module D:/php/php5apache2.dll
  10. 上twitter_如何在Twitter上阻止某人
  11. 12306对抢票软件“下手”了
  12. 移动信号e经常无服务器,手机信号从4G变成E,是什么情况?移动客服作出解答...
  13. 【梁小国】教你怎么签自己的名字
  14. Redis 进阶篇:发布订阅模式原理与运用
  15. 分布式 b2b b2c o2o电子商务 云平台
  16. vs2008与vss2005用后感
  17. GPS北斗时钟服务器(NTP网络时钟系统)施工部署方案
  18. zdog+anime跳舞的小星星动画js特效
  19. 自己动手搭建一个OA办公系统,可行吗?
  20. 2019春季学期总结

热门文章

  1. 树莓派有线网络设置_树莓派的基本网络配置
  2. dedecms友情链接字数修改
  3. php调用shell执行scp,使用shell脚本自动执行scp文件传输
  4. c语言文件尾没有newline字符,关于C++:”文件末尾无新行”编译器警告“No newline at end of file”...
  5. SpringBoot整合定时任务和邮件发送(邮箱 信息轰炸 整蛊)
  6. 在 uni-app 中 使用字体图标
  7. php rsa 跨平台问题,为啥 rsa 这种算法扩展 php/python 不自带。而且跨平台也不是处理的很好...
  8. 计算机上哪个键可以按出符号,键盘符号怎么打出来_各种符号在键盘上怎么打出来-win7之家...
  9. matlab 稳定系统,matlab分析系统的稳定性
  10. 【论文阅读】 Object Detection in 20 Years: A Survey