Java Language Changes for Java SE 9

Java9引入了module模塊的概念,是類與接口和數據資源的一種封裝,並能夠聲明與其餘模塊的依賴關係。這裏總結一下Java9帶來的新特性。html

更簡練的try-with-resources語句java

  final Resource resource1 = new Resource("resource1");//a final resource
  Resource resource2 = new Resource("resource2");//An effectively final resource
  try(resource1;resource2){}//in Java7&8: try(Resource r1=resource1;Resources r2=resource2){}
  //@SafeVarargs可用於static、final、private方法,Iterator<String> i=new Iterator<>(){}鑽石符號可用於匿名類,下劃線不能再用於變量名
集合工廠方法加強
  List.of(E e1, E e2, E e3); //有多個重載方法,老版本:Arrays.asList(「a」,」b」);Stream.of(「a」,」b」).collect(toList())
  Set.of(E … elements); //of工廠方法建立的都是unmodifiable不可更改的集合,可以使用new ArrayList(unmodifiable)再次包裝
  Map.of(K k1, V v1, K k2, V v2); //ofEntries(Map.Entry<K,V> … entries),Map.entry(「K」,」V」)能夠提供entryies參數
  //內部實現使用了wrapper對象,原始集合仍是能夠更改但不建議,unmodifiable集合可用於並行計算,避免不少場合下的集合複製,多個重載方法是爲更好性能
進程ProcessHandle加強
  Process p=new ProcessBuilder(「notepad.exe」).start();//老版本:Runtime.getRuntime().exec(String),Process.waitFor()進程退出後纔會執行後續代碼
  ProcessHandle ph = Process.toHandle(); //long getPid()等方法直接代理給了ProcessHandle.getPid(),當前進程ProcessHandle.current()
  Info i=ProcessHandle.info(); //Info包含了進程命令信息,Optional<String[]> args=Info.arguments(); 命令:Info.command().orElse(「」)
  //onExit().thenAccept(ph –> System.out.println(「PID %d terminated%n」, ph.getPid())); 使用lamda響應進程退出信息,onExit().get()等待進程
多版本三方包Multi-Release Jar
  META-INFO/MANIFEST.MF,包含條目 Multi-Release: true
  META-INFO/releases/9/A.class,在JDK9時可替代/A.class,JDK10則對應10目錄
  //例如獲取pid的不一樣方法,可提供多JDK支持的jar包,JarFile.isMultiRelease()
  //JDK8及之前:jvmName = ManagementFactory.getRuntimeMXBean().getName(); index = jvmName.indexOf('@'); pid=index<1?0:jvmName.substring(0, index);
  //JDK9及之後:ProcessHandle.current().getPid();
jar cfe pid.jar PrintPID -C v1 PrintPID.class -C v1 Util.class --release 9 -C v2 Util.class //java –jar pid.jar,支持JDK8和JDK9
調用棧訪問StackWalker
  StackTraceElement[] stackTrace = new Throwable().getStackTrace(); //老的方式比較耗性能
  StackWalker sw2 = StackWalker.getInstance(); //options可選RETAIN_CLASS_REFERENCE、SHOW_HIDDEN_FRAMES
  sw.forEach(System.out::println); //Consumer<? super StackWalker.StackFrame> action,StackFrame支持fileName、methodName、lineNumber等信息
long numFrames = sw.walk(frames_ -> frames_.count()); //forEach()等價於walk(s -> { s.forEach(action); return null; });walk(s -> { s.forEach(action); return null; });
發佈訂閱框架發佈訂閱框架
SubmissionPublisher<String> publisher = new SubmissionPublisher<>();
MySubscriber<String> subscriber = new MySubscriber<>(); //class MySubscriber<T> implements Subscriber<T>{void onSubscribe(Subscription subscription)}class MySubscriber<T> implements Subscriber<T>{void onSubscribe(Subscription subscription)}void onSubscribe(Subscription subscription)}}}
publisher.subscribe(subscriber); publisher.submit(「A」); publisher.close();publisher.submit(「A」); publisher.close();publisher.close();
相關文章
相關標籤/搜索