千呼萬喚始出來的Java SE 7

3,999 views
3,864 views

Published on




走過漫漫長路,Java SE 7 終於推出,帶來的東西不多也不少,閱讀 JSR 了解新功能總是無趣,選擇這一小時議程,快速體會 777(7 月 7 日 Java SE 7) 即將帶來的欣喜。

Published in: Technology, News & Politics
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,999
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
153
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

千呼萬喚始出來的Java SE 7

  1. 1. 千呼萬喚始出來的 Java SE 7 <ul><li>林信良 </li></ul><ul><li>[email_address] </li></ul><ul><li>http://openhome.cc </li></ul>
  2. 2. 議程 <ul><li>Java 進化史 </li></ul><ul><li>JSR334 / Coin 專案 </li></ul><ul><li>Unicode 6.0.0 、 JDBC 4.1 </li></ul><ul><li>JSR203 / NIO.2 </li></ul><ul><li>JSR166y / Concurrency Updates </li></ul><ul><li>JSR292 / Da Vinci Machine 專案 </li></ul>
  3. 3. Java 進化史 ? 2011/07/28 2002/02/13 2004/09/29 2006/12/11
  4. 4. A / B 計畫 <ul><li>JDK 7 Features updated ! Plan B has apparently been approved </li></ul><ul><ul><li>http://www.baptiste-wicht.com/2010/09/jdk-7-features-updated-plan-b-is-apparently-here/ </li></ul></ul>
  5. 5. JDK7 ... λ Language support for collections Annotations on Java types Jigsaw
  6. 6. JDK7 有 ... <ul><li>JDK 7 Features </li></ul><ul><ul><li>http://openjdk.java.net/projects/jdk7/features/ </li></ul></ul>NIO.2 Coin Invoke Dynamic Concurrency Updates
  7. 7. JSR334 / Coin 專案
  8. 8. Small language enhancements <ul><li>Strings in switch </li></ul><ul><li>Binary integral literals and underscores in numeric literals </li></ul><ul><li>Multi-catch and more precise rethrow </li></ul><ul><li>Improved Type Inference for Generic Instance Creation (diamond) </li></ul><ul><li>try-with-resources statement </li></ul><ul><li>Simplified Varargs Method Invocation </li></ul>
  9. 9. Strings in if-else
  10. 10. Strings in switch
  11. 11. 反組譯 ... 先用 hashCode() 再用 equals()
  12. 12. Binary integral literals and underscores in numeric literals <ul><li>Binary literal </li></ul><ul><li>With underscores for clarity </li></ul>
  13. 13. Multi-catch 都作一樣的事 略 ... 略 ...
  14. 14. Multi-catch <ul><li>It is a compile-time error if a disjunctive type contains two alternatives D i, D j where D i is a subtype of D j . </li></ul>
  15. 15. Multi-catch <ul><li>An exception parameter whose type is a disjunctive type is implicitly considered to be final . </li></ul>try { /* throws some ReflectiveOperationException */ } catch ( final ClassNotFoundException ex1) { /* body */ } catch ( final IllegalAccessException ex2) { /* body */ }
  16. 16. More precise rethrow <ul><li>JDK6... </li></ul>... ... // 作一些事 沒有宣告 Exception
  17. 17. More precise rethrow <ul><li>JDK7... </li></ul>... ...
  18. 18. Inferring Types with <> <ul><li>沒有 Generics 前 </li></ul><ul><li>有了 Generics 後 </li></ul><ul><li>有了 Inferring Types 後 </li></ul>
  19. 19. Inferring Types with <> <ul><li>僅使用 <> 表示推斷類型 </li></ul><ul><li>不過別什麼都交給編譯器判斷 .... </li></ul>
  20. 20. Inferring Types with <> <ul><li>如果 ... </li></ul><ul><li>推斷出來是 Integer 或 Number ? </li></ul>
  21. 21. Inferring Types with <> <ul><li>這有什麼問題? </li></ul>
  22. 22. Try-with-resources <ul><li>讓這段程式碼更耐用一些 ... </li></ul>
  23. 23. Try-with-resources <ul><li>讓這段程式碼 更簡潔一些 ... </li></ul>
  24. 24. Try-with-resources try ResourceSpecification    Block ⇒ {   final VariableModifiers_minus_final R #resource = Expression ;   Throwable #primaryException = null;   try ResourceSpecificationtail      Block   catch (Throwable #t) {     #primaryException = t;     throw #t;   } finally {     if (#resource != null) {       if (#primaryException != null) {         try {           #resource.close();         } catch(Throwable #suppressedException) {           #primaryException.addSuppressed(#suppressedException);         }       } else {         #resource.close();       }     }   } }
  25. 25. Try-with-resources <ul><li>如果要處理例外 ... </li></ul>try-with-resources
  26. 26. Try-with-resources <ul><li>API 支援 </li></ul><ul><ul><li>java.lang.AutoClosable </li></ul></ul><ul><ul><li>java.io.Closeable 繼承 AutoCloseable </li></ul></ul>
  27. 27. Try-with-resources <ul><li>API 支援 </li></ul><ul><ul><li>java.beans.XMLDecoder </li></ul></ul><ul><ul><li>java.beans.XMLEncoder </li></ul></ul><ul><ul><li>java.io.Closeable </li></ul></ul><ul><ul><li>java.io.ObjectInput </li></ul></ul><ul><ul><li>java.io.ObjectOutput </li></ul></ul><ul><ul><li>java.sql.Connection </li></ul></ul><ul><ul><li>java.sql.ResultSet </li></ul></ul><ul><ul><li>java.sql.Statement </li></ul></ul><ul><ul><li>java.nio.channels.FileLock </li></ul></ul><ul><ul><li>javax.sound.midi.MidiDevice </li></ul></ul><ul><ul><li>javax.sound.midi.Receiver </li></ul></ul><ul><ul><li>javax.sound.midi.Transmitter </li></ul></ul><ul><ul><li>javax.sound.sampled.Line </li></ul></ul>
  28. 28. Try-with-resources <ul><li>a 與 b 關閉的順序? </li></ul>
  29. 29. Heap pollution <ul><li>JDK6.... </li></ul><ul><li>OK... </li></ul>要求編譯器檢查傳入引數是不是都是 T 型態
  30. 30. Heap pollution <ul><li>希望編譯器檢查都是 List<String> .... </li></ul><ul><li>編譯器提醒 doSome() 內部實作可能對每個引數,只當作 List ... </li></ul>.....: warning: [unchecked] unchecked generic array creation of type java .util.List<java.lang.String>[] for varargs parameter Util.doSome(Arrays.asList(&quot;three&quot;, &quot;four&quot;), Arrays.asList(“five&quot;, “six&quot;)); ^ 1 warning
  31. 31. Heap pollution <ul><li>執行時期 無法具體確認 ( reified ) 引數或變數型態 </li></ul><ul><ul><li>http:// en.wikipedia.org/wiki/Heap_pollution </li></ul></ul><ul><li>Java 的 Generics 是 編譯時期 檢查 </li></ul><ul><li>Generics 的物件相等性? </li></ul><ul><ul><li>http://caterpillar.onlyfun.net/JavaEssence/ GenericEquals.html </li></ul></ul>
  32. 32. Simplified Varargs Method Invocation <ul><li>JDK7... </li></ul>....: warning: [unchecked] Possible heap pollution from parameterized var arg type T public static <T> List<T> doSome(T... t) { ^ where T is a type-variable: T extends Object declared in method <T>doSome(T...) 1 warning 提醒設計 doSome() 保證不會發生 Heap Pollution
  33. 33. Simplified Varargs Method Invocation <ul><li>@SuppressWarnings( “ unchecked ” ) ? </li></ul><ul><ul><li>連同其它的警訊也抑制了 ... </li></ul></ul><ul><ul><li>對 API 使用者也是有警訊 ... </li></ul></ul><ul><li>@SafeVarargs </li></ul><ul><li>javac -Xlint:varargs; </li></ul>
  34. 34. Simplified Varargs Method Invocation <ul><li>JDK7... </li></ul>
  35. 35. Unicode 6.0.0 <ul><li>2010-10-11 正式發佈 </li></ul>繪文字 ( emoji )
  36. 36. Unicode 6.0.0 <ul><li>Java SE 7 </li></ul><ul><ul><li>Unicode 5.1.0  Unicode 6.0.0 </li></ul></ul><ul><li>Java Tutorial </li></ul><ul><ul><li>http://download.oracle.com/javase/tutorial/i18n/text/unicode.html </li></ul></ul><ul><li>Internationalization Enhancements in Java SE 7 </li></ul><ul><ul><li>http://download.java.net/jdk7/docs/technotes/guides/intl/enhancements.7.html </li></ul></ul>
  37. 37. JDBC 4.1 <ul><li>繼承 AutoCloseable </li></ul><ul><ul><li>Connection </li></ul></ul><ul><ul><li>ResultSet </li></ul></ul><ul><ul><li>Statement </li></ul></ul>
  38. 38. JDBC 4.1 <ul><li>RowSetProvider </li></ul><ul><li>RowSetFactory </li></ul>java -D javax.sql.rowset.RowSetFactory =com.sun.rowset.RowSetFactoryImpl
  39. 39. JSR203 / NIO.2
  40. 40. NIO <ul><li>java.nio </li></ul><ul><ul><li>Buffer </li></ul></ul><ul><ul><li>Channel </li></ul></ul><ul><ul><li>Selector </li></ul></ul><ul><ul><li>Charset </li></ul></ul>NIO2 <ul><li>File System API </li></ul><ul><li>Channels API </li></ul><ul><ul><li>Updates to socket channel API </li></ul></ul><ul><ul><li>Asynchronous I/O </li></ul></ul>
  41. 41. File System API <ul><li>java.io.File 怎麼了 ? </li></ul><ul><ul><li>Not initially written to be extended </li></ul></ul><ul><ul><li>Many of the methods without exceptions </li></ul></ul><ul><ul><li>Methods behaved inconsistently across volumes and file systems </li></ul></ul><ul><ul><li>Methods for gaining simultaneous metadata about files were inefficient </li></ul></ul><ul><ul><li>Developers also requested the ability to develop their own file system implementations, for examples, zip files </li></ul></ul><ul><ul><li>... </li></ul></ul>
  42. 42. File System API <ul><li>新套件 </li></ul><ul><ul><li>java.nio.file </li></ul></ul><ul><ul><li>java.nio.file.attribute </li></ul></ul><ul><li>Java Tutorials: File I/O (Featuring NIO.2) </li></ul><ul><ul><li>http://download.oracle.com/javase/tutorial/essential/io/fileio.html </li></ul></ul>
  43. 43. 關於 Path <ul><li>Methods to access components </li></ul><ul><li>Methods to test and compare </li></ul><ul><li>Methods to combine paths </li></ul><ul><li>File operations </li></ul><ul><li>All methods that access file system throw IOException </li></ul><ul><li>No other checked exceptions in API </li></ul>
  44. 44. 關於 Path <ul><li>建立 Path </li></ul><ul><li>囉嗦點的話 ... </li></ul>
  45. 45. 關於 Path <ul><li>Path 操作 </li></ul><ul><li>實作 equals() </li></ul><ul><li>實作 Iterable </li></ul>同一階層 path.equals(otherPath) Path path = ...; for (Path name: path) { System.out.println(name); }
  46. 46. 檔案移動與複製 <ul><li>支援選項設定 </li></ul><ul><ul><li>copyTo(target, option...) </li></ul></ul><ul><ul><li>moveTo(target, option...) </li></ul></ul>
  47. 47. 檔案屬性( Metadata ) <ul><li>BasicFileAttributes </li></ul>
  48. 48. 檔案屬性( Metadata ) <ul><li>DosFileAttributes </li></ul><ul><li>PosixFileAttributes </li></ul>
  49. 49. 檔案屬性( Metadata ) <ul><li>設定群組、擁有者 ... </li></ul><ul><li>建立 Symbolic link 、 Hard link... </li></ul><ul><li>尋找 Link 的 Target... </li></ul>
  50. 50. 檔案讀寫 <ul><li>針對 java.io 的 API </li></ul>
  51. 51. 檔案讀寫 <ul><li>針對 Channels 與 ByteBuffers </li></ul>
  52. 52. 目錄讀取 <ul><li>使用 Glob 過濾目錄 </li></ul><ul><li>What is Glob </li></ul><ul><ul><li>http://download.oracle.com/javase/tutorial/essential/io/fileOps.html#glob </li></ul></ul>
  53. 53. FileVisitor
  54. 54. FileVisitor 啟始目錄 目錄 檔案 鏈結 檔案 檔案 visitFile visitFile visitFile visitFile preVisitDirectory preVisitDirectory postVisitDirectory postVisitDirectory
  55. 55. 監看目錄修改 <ul><li>WatchService </li></ul>
  56. 56. 新舊之間 <ul><li>Legacy File I/O Code </li></ul><ul><ul><li>http://download.oracle.com/javase/tutorial/essential/io/legacy.html </li></ul></ul>
  57. 57. Asynchronous Channel API <ul><li>java.nio.channels </li></ul><ul><ul><li>AsynchronousSocketChannel </li></ul></ul><ul><ul><li>AsynchronousServerSocketChannel </li></ul></ul><ul><ul><li>AsynchronousFileChannel </li></ul></ul><ul><ul><li>AsynchronousDatagramChannel </li></ul></ul>
  58. 58. Asynchronous Channel API <ul><li>建立 AsychronousServerSocketChannel </li></ul><ul><ul><li>類似建立 ServerSocketChannel 並綁定位址 </li></ul></ul><ul><li>接受連結 </li></ul>
  59. 59. Asynchronous Channel API <ul><li>Future 模式 </li></ul>
  60. 60. Asynchronous Channel API <ul><li>客戶端建立 AsynchronousSocketChannel </li></ul><ul><li>開啟連結 </li></ul>Future<Void>
  61. 61. Asynchronous Channel API <ul><li>客戶端傳送訊息 </li></ul><ul><li>伺服端讀取訊息 </li></ul>
  62. 62. Asynchronous Channel API <ul><li>The asynchronous channel APIs </li></ul><ul><ul><li>http://www.ibm.com/developerworks/java/library/j-nio2-1/index.html </li></ul></ul>
  63. 63. JSR166y / Concurrency Updates
  64. 64. Concurrency Updates <ul><li>JDK5 </li></ul><ul><ul><li>JSR 166: Concurrency Utilities </li></ul></ul><ul><li>JDK7 </li></ul><ul><ul><li>JSR166y: Concurrency and collections updates </li></ul></ul><ul><ul><li>java.util.concurrent </li></ul></ul><ul><ul><ul><li>ForkJoinPool </li></ul></ul></ul><ul><ul><ul><li>Phaser </li></ul></ul></ul><ul><ul><ul><li>TransferQueue </li></ul></ul></ul><ul><ul><ul><li>ConcurrentLinkedDeque </li></ul></ul></ul><ul><ul><ul><li>ThreadLocalRandom </li></ul></ul></ul><ul><ul><ul><li>... </li></ul></ul></ul>
  65. 65. Divide and conquer
  66. 66. Fork / Join 模式 <ul><li>Divide and conquer 的一種實現策略 </li></ul><ul><li>著重在如何切割任務與組合子任務結果 </li></ul>
  67. 67. Fork / Join 框架 不需傳回值的子動作 透過泛型傳回值的子任務
  68. 68. Fork / Join 框架 <ul><li>ForkJoinTask :類似執行緒的輕量級實體 </li></ul><ul><li>有別於 ExecutorService 的 ForkJoinPool </li></ul><ul><ul><li>採用 Work-stealing 演算,讓空閒的執行緒從執行較慢的執行緒中偷取任務 </li></ul></ul><ul><ul><li>建構與處理器數量相當執行緒,減少頻繁切換負擔 </li></ul></ul>
  69. 69. Fibonacci 結果 1134903170 耗時 24608
  70. 70. Fibonacci .... you'd pick some minimum granularity size for which you always sequentially solve rather than subdividing. 結果 1134903170 耗時 199604
  71. 71. 搜尋檔案 結果 665 耗時 12813
  72. 72. 搜尋檔案 結果 665 耗時 7596
  73. 73. JSR292 / Da Vinci Machine 專案
  74. 74. 靜態定型語言 <ul><li>根據資料的型態資訊,將變數及運算式進行分類, 型態資訊是在宣告變數上 </li></ul><ul><li>在執行時期變數的型態資訊無法改變,資料只能被指定至同一種型態的變數 </li></ul>
  75. 75. 靜態定型語言 2: invokestatic #3 // Method doSome:(Ljava/lang/String;)Ljava/lang/Integer; 17: invokevirtual #7 // Method Other.doOther:(Ljava/lang/String;)Ljava/lang/Integer; 32: invokeinterface #9, 2 // InterfaceMethod Another.doAnother:(Ljava/lang/String;)Ljava/lang/Integer;
  76. 76. 動態定型語言 <ul><li>型態資訊在資料本身 而不是變數 </li></ul><ul><li>變數本身的型態是在執行時期運算得知,也同一變數可以參考至各種型態的資料 </li></ul>function max(m, n) { if(n.lessThan(m)) { return m; } return n; } def max(m, n): return m if n.lessThan(m) else n JavaScript Python
  77. 77. 動態定型語言的挑戰 <ul><li>參數與傳回值都沒有型態 </li></ul><ul><li>執行時才提供型態資訊 </li></ul><ul><li>無法提前得知呼叫的方法相關型態資訊 </li></ul><ul><li>如何成功編譯為位元碼( Byte code )? </li></ul>function max(m, n) { if( n.lessThan(m )) { return m; } return n; }
  78. 78. 動態定型語言的挑戰 <ul><li>為參數與傳回值建立 合成( synthetic )類型 </li></ul><ul><ul><li>可符合位元碼對型態的要求 </li></ul></ul><ul><ul><li>需要為各種語言建立特定實作( Implementer ),以產生對應類型 </li></ul></ul><ul><ul><li>JVM 本身的類型( String 、 Integer ... )通常也無法直接對應 </li></ul></ul>MyObject max( MyObject x, MyObject y) { if( n.lessThan(m) ) { return n; } return m; } invokevirtual #9 // Method MyObject.lessThan:(Ljava/lang/MyObject;)Ljava/lang/Boolean;
  79. 79. 動態定型語言的挑戰 <ul><li>運用 反射呼叫( reflected invocation ) </li></ul><ul><ul><li>java.lang.reflect.Method 提供某類別或介面的方法直接呼叫(正是特定語言實作器所要求的) </li></ul></ul><ul><ul><li>這些方法仍是執行時期從特定類別上取得 </li></ul></ul><ul><ul><li>執行時期的效能非常不彰 </li></ul></ul>
  80. 80. 動態定型語言的挑戰 <ul><li>在 JVM 上 執行特定語言 直譯器 ( Interpreter ) </li></ul><ul><ul><li>非常非常 .... 慢 ... </li></ul></ul>
  81. 81. JSR 292 <ul><li>為 JVM 與位元碼中新增 invokedynamic 指令 </li></ul><ul><li>新的方法連結機制 </li></ul><ul><ul><li>java.lang.invoke </li></ul></ul><ul><ul><ul><li>CallSite </li></ul></ul></ul><ul><ul><ul><li>MethodHandle </li></ul></ul></ul><ul><ul><ul><li>... </li></ul></ul></ul>invokedynamic <method-specification> <n>
  82. 82. <ul><li>考慮有個語言 ... </li></ul><ul><li>為這段程式碼產生位元碼 ... </li></ul>a = 40; b = a + 2; bipush 40 istore_1 iload_1 invokestatic Method java/lang/Integer.valueOf:&quot;(I)Ljava/lang/Integer;&quot;; bipush 2 invokestatic Method java/lang/Integer.valueOf:&quot;(I)Ljava/lang/Integer;&quot;; invokedynamic InvokeDynamic REF_invokeStatic: Example.mybsm: &quot;(Ljava/lang/invoke/MethodHandles/Lookup; Ljava/lang/String; Ljava/lang/invoke/MethodType;) Ljava/lang/invoke/CallSite;&quot;: +: &quot;(Ljava/lang/Integer; Ljava/lang/Integer;) Ljava/lang/Integer;&quot;; the invokedynamic call site is + method implementation currently unknown bootstrap method
  83. 83. JSR 292 <ul><li>+ 對整數的方法實作 </li></ul>
  84. 84. <ul><li>An invokedynamic call site is linked to a method by means of a bootstrap method </li></ul>類似 Reflection ?
  85. 85. JSR 292 <ul><li>java.lang.reflect.Method </li></ul><ul><ul><li>非方法呼叫專用 ,執行時期有許多不必要資訊 </li></ul></ul><ul><ul><li>重量級,速度慢 </li></ul></ul><ul><li>java.lang.invoke.MethodHandle </li></ul><ul><ul><li>主要目的就是在方法呼叫 </li></ul></ul><ul><ul><li>輕量級,速度快 </li></ul></ul>
  86. 86. JSR 292 <ul><li>Java Virtual Machine Support for Non-Java Languages </li></ul><ul><ul><li>http://download.java.net/jdk7/docs/technotes/guides/vm/multiple-language-support.html </li></ul></ul>
  87. 87. JSR 292 <ul><li>If a programming language was a boat… </li></ul><ul><ul><li>http://compsci.ca/blog/if-a-programming-language-was-a-boat/ </li></ul></ul>
  88. 88. Recently rediscovered diagrams prove Leonardo invented the first JVM ? . http://openjdk.java.net/projects/mlvm/ 感謝 <ul><li>林信良 </li></ul><ul><li>[email_address] </li></ul><ul><li>http://openhome.cc </li></ul>Orz...

×