当前位置: 首页 > news >正文

北海哪家做网站网站管理cms

北海哪家做网站,网站管理cms,网站上广告,公司起名字大全免费四字原文网址:Java多线程系列--synchronized的原理_IT利刃出鞘的博客-CSDN博客 简介 本文介绍Java的synchronized的原理。 反编译出字节码 Test.java public class Test {private static Object LOCK new Object();public static int main(String[] args) {synchro…

原文网址:Java多线程系列--synchronized的原理_IT利刃出鞘的博客-CSDN博客

简介

        本文介绍Java的synchronized的原理。

反编译出字节码

Test.java

public class Test {private static Object LOCK = new Object();public static int main(String[] args) {synchronized (LOCK){System.out.println("Hello World");}return 1;}
}

先用javac Test.class 编译出class文件
再用javap –c Test.class查看字节码文件

字节码文件:

也就是说,锁是通过monitorenter和monitorexit来实现的。

进入监视器

JVM规范中描述:

    monitorenter:` Each object is associated with a monitor. A monitor is locked if and only if it has an owner. The thread that executes monitorenter attempts to gain ownership of the monitor associated with objectref, as follows: • If the entry count of the monitor associated with objectref is zero, the thread enters the monitor and sets its entry count to one. The thread is then the owner of the monitor. • If the thread already owns the monitor associated with objectref, it reenters the monitor, incrementing its entry count. • If another thread already owns the monitor associated with objectref, the thread blocks until the monitor’s entry count is zero, then tries again to gain ownership. `

翻译:

        每个对象有一个监视器锁(monitor)。当monitor被占用时就会处于锁定状态,线程执行monitorenter指令时尝试获取monitor的所有权,过程如下:

  1. 如果monitor的进入数为0,则该线程进入monitor,然后将进入数设置为1,该线程即为monitor的所有者。
  2. 如果线程已经占有该monitor,只是重新进入,则进入monitor的进入数加1.
  3. 如果其他线程已经占用了monitor,则该线程进入阻塞状态,直到monitor的进入数为0,再重新尝试获取monitor的所有权。

退出监视器

JVM规范中描述:

        monitorexit:  ` The thread that executes monitorexit must be the owner of the monitor associated with the instance referenced by objectref. The thread decrements the entry count of the monitor associated with objectref. If as a result the value of the entry count is zero, the thread exits the monitor and is no longer its owner. Other threads that are blocking to enter the monitor are allowed to attempt to do so.`       

翻译:

        执行monitorexit的线程必须是objectref所对应的monitor的所有者。

        指令执行时,monitor的进入数减1,如果减1后进入数为0,则线程退出monitor,不再是这个monitor的所有者。此时,其他被这个monitor阻塞的线程可以尝试去获取这个 monitor 的所有权。

        Synchronized的语义底层是通过一个monitor的对象来完成,其实wait/notify等方法也依赖于monitor对象,这就是为什么只有在同步的块或者方法中才能调用wait/notify等方法,否则会抛出java.lang.IllegalMonitorStateException的异常的原因。

http://www.yayakq.cn/news/628535/

相关文章:

  • 招聘信息网站开发背景应届生去外包公司
  • 无锡做网站好网站建设费是广告费吗
  • 南通企业网站排名优化h5可以做网站吗
  • 旅游网站怎么做才能被关注免费 开源 企业网站
  • 怎么吧自己电脑做网站二级学院网站建设报告
  • 手机制作网站主页软件字母logo设计网站
  • cargo创建个人网站昆明网站建设搜q479185700
  • 食品包装设计网站wordpress代码块行号
  • 杭州微信建站深圳自助网站建设费用
  • 上海建设局网站 招聘网站开发文档需求撰写word
  • 建设通是正规网站吗计算机网站开发岗位有哪些
  • 东莞网站推广优化网站网站开发安全
  • 8网站免费建站怎么做网站流量统计分析
  • 电子商务网站网站建设网站建设保教
  • 做网站 客户一直要求改泉州那家做网站公司好
  • 母版页和窗体做网站例子大连企业网站设计
  • 西安优惠电商平台网站上海高中生做课题的网站
  • php企业网站demothinkphp企业网站系统
  • 网站备案查询中心旅游平台网站合作建设方案
  • 保定建设银行网站首页商业平台网站开发
  • 网站开发的好处和弊端怎么自建导购网站做淘客
  • 国外网站 服务器外包服务平台
  • 国内永久免费域名申请网站更换dns能上国外网站吗
  • 嘉兴建设教育网站建盏公司最新消息
  • 自己创建个人免费网站天元建设集团官网
  • 越秀手机网站建设公司网站建设预算
  • 网站建设及维护价钱推广网店的途径和方法
  • 网站建设1wordpress一键采集文章
  • 做任务分享赚钱的网站阳江房产网签
  • 为什么选择当网站设计人员如何用织梦猫做网站和后台