专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »Java教程 » javajvm:Java Thread in JVM »正文

javajvm:Java Thread in JVM

来源: 发布时间:星期三, 2008年12月17日 浏览:2次 评论:0
本文从JVM角度探讨Java Thread语法和编译结果如果需要获得第手资料请直接访问以下资源——Java语言规范标准Java虚拟机规范标准中有关线程定义介绍说明 本文旨在介绍这些比较重要线程相关规范标准基本上不另作发挥(除了提到微软“公共语言基础构造”:-) Java Language Specication http://java.sun.com/docs/books/jls/second_edition/html/es.doc.html#30531 JVM Specication 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/es.doc.html#30531 用synchronized关键字修饰思路方法分为两种情况:()静态思路方法和例子思路方法 ()静态思路方法“锁”是这个拥有这个思路方法对象Class对象;例子思路方法“锁”是this拥有这个思路方法当前对象例子 如何理解这段话看下面例子就明白了 下面两段代码效果完全相同代码1 代码2 代码1: Test { count; synchronized void bump { count; } Count; synchronized void Bump { Count; } } 代码2: BumpTest { count; void bump { synchronized (this) { count; } } Count; void Bump { try { synchronized (Class.forName("BumpTest")) { Count; } } 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.doSomethingV 8 aload_2 // Push local variable 2 (f) 9 monitorexit // Exit the monitor associated with f 10 // Return normally 11 aload_2 // In 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, ting the entry count of this monitor to 1. 这段话意思是说monitorenter操作目标定要是个对象类型是referenceReference实际就是堆里个存放对象地址每个对象(reference)都有个monitor对应如果有其它线程获取了这个对象monitor当前线程就要直等待直到获得monitor线程放弃monitor当前线程才有机会获得monitor 如果monitor没有被任何线程获取那么当前线程获取这个monitor把monitorentry 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 specication? 本文主要讨论java相关规范标准内容规范标准文档非常重要尤其对于javaC#这种生成中间代码语言来说 上面说是java相关规范标准这里顺便提下微软.Net相关规范标准 微软“公共语言基础构造”规范标准: Microsoft CLI -- Common Language Infrastructure (sorry, off the topic :-) http://msdn.microsoft.com/net/ecma/ 这个网址上有C#语言规范标准CLI规范标准下载 Enjoy it. :-)

相关文章

读者评论

  • 共0条 分0页

发表评论

  • 昵称:
  • 内容: