久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Java Thread in JVM

瀏覽:4日期:2024-07-01 09:45:18
內(nèi)容: 本文從JVM的角度探討Java Thread的語(yǔ)法和編譯結(jié)果。如果需要獲得第一手資料,請(qǐng)直接訪問以下的資源——Java語(yǔ)言規(guī)范,Java虛擬機(jī)規(guī)范中有關(guān)線程的定義說(shuō)明。本文旨在介紹這些比較重要的線程相關(guān)的規(guī)范,基本上不另作發(fā)揮。(除了提到微軟的“公共語(yǔ)言基礎(chǔ)構(gòu)造。:-)Java Language Specificationhttp://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#30531JVM Specificationhttp://java.sun.com/docs/books/vmspec/2nd-edition/html/Compiling.doc.html#6530http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc9.htmlhttp://java.sun.com/docs/books/vmspec/2nd-edition/html/Threads.doc.htmlMicrosoft CLI -- Common Language Infrastructure (sorry, off the topic :-)http://msdn.microsoft.com/net/ecma/1.synchronized method 的java語(yǔ)言規(guī)范詳見http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#30531。用synchronized關(guān)鍵字修飾的方法,分為兩種情況:(static)靜態(tài)方法,和實(shí)例方法。(static)靜態(tài)方法的“鎖是這個(gè)擁有這個(gè)方法的對(duì)象的Class對(duì)象;實(shí)例方法的“鎖是this,擁有這個(gè)方法的當(dāng)前對(duì)象實(shí)例。怎么理解這段話,看一看下面的例子就明白了。下面兩段代碼的效果完全相同。代碼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關(guān)鍵字的編譯結(jié)果這一節(jié),我們來(lái)看一看synchronized關(guān)鍵字編譯之后的java虛擬機(jī)指令是什么。如果需要第一手資料,請(qǐng)參見java虛擬機(jī)規(guī)范相關(guān)的部分http://java.sun.com/docs/books/vmspec/2nd-edition/html/Compiling.doc.html#6530這段規(guī)范里面講到,java虛擬機(jī)規(guī)范提供兩條指令,monitorenter和monitorexit,來(lái)支持線程。但是對(duì)于上一節(jié)講到的,用synchronized修飾的方法來(lái)說(shuō),并不使用這兩個(gè)方法,而只是簡(jiǎn)單地用ACC_SYNCHRONIZED標(biāo)志修飾。虛擬機(jī)調(diào)用方法的時(shí)候會(huì)檢查這個(gè)標(biāo)志,進(jìn)行同步。synchronized語(yǔ)句的編譯結(jié)果對(duì)應(yīng)monitorenter和monitorexit兩條指令。比如,下面的代碼:void onlyMe(Foo f) {synchronized(f) {doSomething();}}的編譯結(jié)果是Method void onlyMe(Foo)0 aload_1 // Push f 1 astore_2 // Store it in local variable 22 aload_2 // Push local variable 2 (f)3 monitorenter // Enter the monitor associated with f4 aload_0 // Holding the monitor, pass this and...5 invokevirtual #5 // ...call Example.doSomething()V8 aload_2 // Push local variable 2 (f)9 monitorexit // Exit the monitor associated with f10 return // Return normally11 aload_2 // In case of any throw, end up here12 monitorexit // Be sure to exit monitor...13 athrow // ...then rethrow the value to the invoker3.monitorenter和monitorexit詳見http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc9.htmlmonitorenter定義的一段節(jié)錄:Operation : Enter monitor for objectOperand 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.這段話的意思是說(shuō),monitorenter操作的目標(biāo)一定要是一個(gè)對(duì)象,類型是reference。Reference實(shí)際就是堆里的一個(gè)存放對(duì)象的地址。每個(gè)對(duì)象(reference)都有一個(gè)monitor對(duì)應(yīng),如果有其它的線程獲取了這個(gè)對(duì)象的monitor,當(dāng)前的線程就要一直等待,直到獲得monitor的線程放棄monitor,當(dāng)前的線程才有機(jī)會(huì)獲得monitor。如果monitor沒有被任何線程獲取,那么當(dāng)前線程獲取這個(gè)monitor,把monitor的entry count設(shè)置為1。表示這個(gè)monitor被1個(gè)線程占用了。當(dāng)前線程獲取了monitor之后,會(huì)增加這個(gè)monitor的時(shí)間計(jì)數(shù),來(lái)記錄當(dāng)前線程占用了monitor多長(zhǎng)時(shí)間。我們看到,monitor這個(gè)詞在java虛擬機(jī)規(guī)范規(guī)定出現(xiàn),但是在java語(yǔ)言和API文檔里面并沒有出現(xiàn)。monitor是藏在線程同步后面的原理和概念。4.Threads and Locks詳見http://java.sun.com/docs/books/vmspec/2nd-edition/html/Threads.doc.html。這段規(guī)范詳細(xì)地介紹了thread和lock的原理。下面給出這段規(guī)范的highlight。8.4 Nonatomic Treatment of double and long Variables (double和long類型的非原子操作。)8.7 Rules for volatile Variables8.10 Example: Possible Swap8.11 Example: Out-of-Order Writes如果對(duì)列出的這些highlight感興趣,請(qǐng)?jiān)L問相應(yīng)的java虛擬機(jī)規(guī)范網(wǎng)址。5.Why specification?本文主要討論java相關(guān)規(guī)范的內(nèi)容。規(guī)范文檔非常重要,尤其對(duì)于java,C#這種生成中間代碼的語(yǔ)言來(lái)說(shuō)。上面說(shuō)的是java的相關(guān)規(guī)范。這里順便提一下微軟.Net的相關(guān)規(guī)范。微軟的“公共語(yǔ)言基礎(chǔ)構(gòu)造規(guī)范:Microsoft CLI -- Common Language Infrastructure (sorry, off the topic :-)http://msdn.microsoft.com/net/ecma/這個(gè)網(wǎng)址上有C#語(yǔ)言規(guī)范,CLI規(guī)范的下載。Enjoy it. :-)出處 wang hailong Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd
標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 一级片av | 99精品久久久 | 亚洲国产精品久久人人爱 | 美女午夜视频 | 国产精品婷婷久久久久 | 成人欧美一区二区三区在线播放 | 精品一区二区三区久久久 | 国产亚洲一区在线 | 日韩成人在线网 | 日本福利视频网 | 久久亚洲一区二区 | 国产精品久久久久久久久久久免费看 | 伊人网在线免费观看 | 91看片淫黄大片一级在线观看 | 一级片黄片毛片 | 日韩精品日韩激情日韩综合 | 午夜免费高清视频 | 二区三区 | 精品久久久久久久久久久 | 免费一区| 色欧美视频 | 日韩av手机在线免费观看 | 91精品久久久久久久久久久久久久久 | 欧美视频在线播放 | 日本三级欧美三级 | 久久免费视频3 | 91精品国产综合久久精品 | 玖玖国产精品视频 | 午夜精品一区二区三区四区 | 黄色一级片视频 | 日日日操 | 久在线看 | 免费毛片在线 | 超级乱淫片国语对白免费视频 | 精品视频一区二区三区 | 日本在线免费电影 | 久久涩 | 亚洲精选免费视频 | 最新高清无码专区 | 国产免费视频 | 久久精品久久久久久久久久16 |