国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Java Thread in JVM

2019-11-18 12:13:57
字體:
來源:轉載
供稿:網友

  本文從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. :-)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黎川县| 临颍县| 灵丘县| 南部县| 拉萨市| 尉氏县| 舒城县| 石楼县| 龙门县| 白朗县| 克山县| 岗巴县| 广昌县| 宝清县| 岳普湖县| 衡水市| 千阳县| 苏尼特右旗| 泰和县| 合川市| 淄博市| 抚顺市| 华容县| 黄大仙区| 慈溪市| 荆州市| 秭归县| 盖州市| 绥化市| 玉溪市| 清新县| 虞城县| 鹤壁市| 如东县| 鹿邑县| 望城县| 青冈县| 高邮市| 称多县| 阳谷县| 神池县|