动态网站制作指南 [  QQ表情  ]
[ 投票调查 ]
[ 企业邮箱 ]
[ 网站空间 ]
网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
ASP源码 | .Net源码 | PHP源码 | JSP源码 | JAVA源码 | CGI源码 | VB源码 | C++源码 | Delphi源码 | PB源码 | VF源码 | 汇编 | 服务器
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java教程
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
文章搜索服务
邮件订阅
输入你的邮件地址,
你将不会错过任何关于:
[ Java教程 ]的信息

本月文章推荐
.也来谈Java集合框架.
.运用BitSet类来处理一系列选项.
.java关于日期的运算等处理方法.
.Java中引用变量的使用.
.Eclipse3.1中体验J2SE5.0之注释类.
.使用Spring来创建一个简单的工作.
.Hibernate配置文件中映射元素详解.
.Tomcat在Windows 2000下的安装配.
.关于在Eclipse下使用Subversion教.
.Sun将升级高端Unix服务器 UltraS.
.精通ejb【三】.
.关于Java中文问题的几条分析原则.
.Struts开发指南之其他Web构架介绍.
.Java学习笔记(一)--熟悉开发环.
.面向对象的思维方法.
.instanceof 运算符.
.struts开发实践—分页的实现.
.java中文乱码解决方案和经验.
.用Applet写的菜单程序 machine.
.Hibernate二级缓存攻略.

Java Thread in JVM

发表日期:2008-1-5 |



  本文从JVM的角度探讨Java Thread的语法和编译结果。假如需要获得第一手资料,请直接访问以下的资源——Java语言规范,Java虚拟机规范中有关线程的定义说明。
  
  本文旨在介绍这些比较重要的线程相关的规范,基本上不另作发挥。(除了提到微软的“公共语言基础构造”。:-)
  
  
  
  Java Language Specification
  
  http://java.sun.com/docs/books/jls/second_edition/Html/classes.doc.html#30531
  
  
  
  JVM Specification
  
  http://java.sun.com/docs/books/vmspec/2nd-edition/html/Compiling.doc.html#6530
  
  http://java.sun.com/docs/books/vmspec/2nd-edition/html/InstrUCtions2.doc9.html
  
  http://java.sun.com/docs/books/vmspec/2nd-edition/html/Threads.doc.html
  
  
  
  Microsoft CLI -- Common Language Infrastructure (sorry, off the topic :-)
  
  http://msdn.microsoft.com/net/ecma/
  
  1.synchronized method 的java语言规范
  详见http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#30531。
  
  
  
  用synchronized要害字修饰的方法,分为两种情况:(static)静态方法,和实例方法。
  
  (static)静态方法的“锁”是这个拥有这个方法的对象的Class对象;实例方法的“锁”是this,拥有这个方法的当前对象实例。
  
  怎么理解这段话,看一看下面的例子就明白了。
  
  下面两段代码的效果完全相同。代码1 ==代码2。
  
  代码1:
  
  class Test {
  
  int count;
  
  synchronized void bump() { count++; }
  
  static int classCount;
  
  static synchronized void classBump() {
  
  classCount++;
  
  }
  
  }
  
  代码2:
  
  class BumpTest {
  
  int count;
  
  void bump() {
  
  synchronized (this) {
  
  count++;
  
  }
  
  }
  
  static int classCount;
  
  static void classBump() {
  
  try {
  
  synchronized (Class.forName("BumpTest")) {
  
  classCount++;
  
  }
  
  } catch (ClassNotFoundException e) {
  
  ...
  
  }
  
  }
  
  }
  
  
  
  2.synchronized要害字的编译结果
  这一节,我们来看一看synchronized要害字编译之后的java虚拟机指令是什么。
  
  假如需要第一手资料,请参见java虚拟机规范相关的部分
  
  http://java.sun.com/docs/books/vmspec/2nd-edition/html/Compiling.doc.html#6530
  
  
  
  这段规范里面讲到,java虚拟机规范提供两条指令,monitorenter和monitorexit,来支持线程。但是对于上一节讲到的,用synchronized修饰的方法来说,并不使用这两个方法,而只是简单地用ACC_SYNCHRONIZED标志修饰。虚拟机调用方法的时候会检查这个标志,进行同步。
  
  synchronized语句的编译结果对应monitorenter和monitorexit两条指令。
  
  比如,下面的代码:
  
  void onlyMe(Foo f) {
  
  synchronized(f) {
  
  doSomething();
  
  }
  
  }
  
  的编译结果是
  
  Method void onlyMe(Foo)
  
  0 aload_1 // Push f
  
  1 astore_2 // Store it in local variable 2
  
  2 aload_2 // Push local variable 2 (f)
  
  3 monitorenter // Enter the monitor associated with f
  
  4 aload_0 // Holding the monitor, pass this and...
  
  5 invokevirtual #5 // ...call Example.doSomething()V
  
  8 aload_2 // Push local variable 2 (f)
  
  9 monitorexit // Exit the monitor associated with f
  
  10 return // Return normally
  
  11 aload_2 // In case of any throw, end up here
  
  12 monitorexit // Be sure to exit monitor...
  
  13 athrow // ...then rethrow the value to the invoker
  
  3.monitorenter和monitorexit
  详见http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc9.html
  
  
  
  monitorenter定义的一段节录:
  
  Operation : Enter monitor for object
  
  Operand Stack : ..., objectref ...
  
  Description :
  
  The objectref must be of type reference.
  
  Each object has a monitor associated with it. The thread that executes monitorenter gains ownership of the monitor associated with objectref. If another thread already owns the monitor associated with objectref, the current thread waits until the object is unlocked, then tries again to gain ownership. If the current thread already owns the monitor associated with objectref, it increments a counter in the monitor indicating the number of times this thread has entered the monitor. If the monitor associated with objectref is not owned by any thread, the current thread becomes the owner of the monitor, setting the entry count of this monitor to 1.
  
  
  
  这段话的意思是说,monitorenter操作的目标一定要是一个对象,类型是reference。Reference实际就是堆里的一个存放对象的地址。每个对象(reference)都有一个monitor对应,假如有其它的线程获取了这个对象的monitor,当前的线程就要一直等待,直到获得monitor的线程放弃monitor,当前的线程才有机会获得monitor。
  
  假如monitor没有被任何线程获取,那么当前线程获取这个monitor,把monitor的entry count设置为1。表示这个monitor被1个线程占用了。
  
  当前线程获取了monitor之后,会增加这个monitor的时间计数,来记录当前线程占用了monitor多长时间。
  
  
  
  我们看到,monitor这个词在java虚拟机规范规定出现,但是在java语言和API文档里面并没有出现。monitor是藏在线程同步后面的原理和概念。
  
  4.Threads and Locks
  详见http://java.sun.com/docs/books/vmspec/2nd-edition/html/Threads.doc.html。
  
  这段规范具体地介绍了thread和lock的原理。下面给出这段规范的highlight。
  
  8.4 Nonatomic Treatment of double and long Variables (double和long类型的非原子操作。)
  
  8.7 Rules for volatile Variables
  
  8.10 Example: Possible Swap
  
  8.11 Example: Out-of-Order Writes
  
  假如对列出的这些highlight感爱好,请访问相应的java虚拟机规范网址。
  
  5.Why specification?
  本文主要讨论java相关规范的内容。规范文档非常重要,尤其对于java,C#这种生成中间代码的语言来说。
  
  上面说的是java的相关规范。这里顺便提一下微软.Net的相关规范。
  
  微软的“公共语言基础构造”规范:
  
  Microsoft CLI -- Common Language Infrastructure (sorry, off the topic :-)
  
  http://msdn.microsoft.com/net/ecma/
  
  这个网址上有C#语言规范,CLI规范的下载。
  
  Enjoy it. :-)
上一篇:获取运行中的JVM系统属性 人气:491
下一篇:什么是jvm?你很清楚地了解它吗? 人气:926
浏览全部Java的内容 Dreamweaver插件下载 常用网页广告代码全集
  最新网站源码 最新软件下载
2008-9-4 LPLY CMS 网站管理系统 v5.0
2008-9-4 缤纷互动视频交友 v3.01.902
2008-9-4 ADN视频收藏专家 v3.0 bulid 080
2008-9-4 天空网络电影系统SKYUC v2.5.6 简
2008-9-4 Web Wiz Rich Text Editor(文本编
2008-9-4 幻影动漫网视频系统(Ppdong) v1.
2008-9-4 乐维电脑在线DIY配置系统
2008-9-4 老樊文章管理系统SQL版
2008-9-4 ASP.NET 2.53 缩略图水印组件源码
2008-8-23 Mini WinMount V0.4
2008-8-23 Vista优化大师3.11正式版
2008-8-23 Wine 1.13
2008-8-23 KlipFolio 5.0 Build 5899-80
2008-8-23 Windows Sysinternals Desktops
2008-8-23 OneTap Movies1.2破解版
2008-8-23 AnnotaterPDF阅读1.1.503 破解版
2008-8-23 SoundMeter分贝测量仪 v1.0汉化破
2008-8-23 iDrum音乐节拍1.0破解版
  发表评论
姓 名: 验证码:
内 容:
站长工具:网站收录查询 | Google PR查询 | ALEXA排名查询 | CSS在线编辑器 | 广告代码 | Html转换js | js/vbs加密 | md5加密 | 进制转换
实用工具:汉字翻译拼音 | 符号对照表 | 个税计算 | 经典小工具 | 汉字简繁转换 | 普通单位换算 | 公制单位换算 | 生辰老黄历 | 国内电话区号 国家代码与域名缩写 | 文字加密解密 | 健康查询 | 万年历 | 汉字横竖排版 | 手机号码查询 | 计算器 | ip搜索
业务联系 | 广告刊登 | 频道合作 | 投稿荐稿 | 联系方式 | 加入收藏 | RSS订阅
Copyright © 2000-2008 www.knowsky.com All rights reserved | 网络实名:动态网站制作指南 | 沪ICP备05001343号
ホームページ制作 不動産検索システム 求人情報
防水工事·改修工事 フットサル大会 探偵