电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

类型Java程序高手进阶之Java19的新特性介绍

收藏

编号:337994252    类型:共享资源    大小:106.43KB    格式:PDF    上传时间:2022-10-11
  
2
金贝
分享到微信 分享到微博 分享到QQ空间
关 键 词:
Java 程序 高手 进阶 Java19 特性 介绍
资源描述:
序序本文主要讲述一下Java19的新特性版本号版本号java-versionopenjdk version 19 2022-09-20OpenJDK Runtime Environment(build 19+36-2238)OpenJDK 64-Bit Server VM(build 19+36-2238,mixed mode,sharing)从version信息可以看出是build 19+36特性列表特性列表JEP 405:Record Patterns(Preview)JEP 405:Record Patterns(Preview)instanceof的模式匹配在JDK14作为preview,在JDK15作为第二轮的preview,在JDK16JEP 转正394:Pattern Matching for instanceofswitch模式匹配在JDK17的引入作为JEP 406:Pattern Matching for switch(Preview)preview版本,在JDK18的作为第JEP 420:Pattern Matching for switch(Second Preview)二轮的preview针对record类型,引入instance of可以这么写record Point(int x,int y)static void printSum(Object o)if(o instanceof Point p)int x=p.x();int y=p.y();System.out.println(x+y);现在可以这么写record Point(int x,int y)void printSum(Object o)if(o instanceof Point(int x,int y)System.out.println(x+y);比较复杂的例子:record Point(int x,int y)enum Color RED,GREEN,BLUE record ColoredPoint(Point p,Color c)record Rectangle(ColoredPoint upperLeft,ColoredPoint lowerRight)Rectangle r=new Rectangle(new ColoredPoint(new Point(x1,y1),c1),new ColoredPoint(new Point(x2,y2),c2);static void printXCoordOfUpperLeftPointWithPatterns(Rectangle r)if(r instanceof Rectangle(ColoredPoint(Point(var x,var y),var c),var lr)System.out.println(Upper-left corner:+x);如果是泛型record的话:record Box(T t)static void test1(Box bo)if(bo instanceof Box(String s)System.out.println(String +s);static void test2(Box bo)if(bo instanceof Box(var s)System.out.println(String +s);JEP 422:Linux/RISC-V PortJEP 422:Linux/RISC-V PortRISC-V是一个基于精简指令集(RISC)原则的开源指令集架构(ISA),这个JEP则移植JDK到RISC-V上JEP 424:Foreign Function&Memory API(Preview)JEP 424:Foreign Function&Memory API(Preview)Foreign Function&Memory(FFM)API包含了两个incubating APIJDK14的引入了Foreign-Memory Access JEP 370:Foreign-Memory Access API(Incubator)API作为incubatorJDK15的Foreign-Memory JEP 383:Foreign-Memory Access API(Second Incubator)Access API作为第二轮incubatorJDK16的作为第三轮,它引入了JEP 393:Foreign-Memory Access API(Third Incubator)Foreign Linker API(JEP)389FFM API在JDK 17的作为incubatorJEP 412:Foreign Function&Memory API(Incubator)引入FFM API在JDK 18的作为第JEP 419:Foreign Function&Memory API(Second Incubator)二轮incubatorJDK19的这个JEP则将FFM API作为preview API使用示例/1.Find foreign function on the C library pathLinker linker=Linker.nativeLinker();SymbolLookup stdlib=linker.defaultLookup();MethodHandle radixSort=linker.downcallHandle(stdlib.lookup(radixsort),.);/2.Allocate on-heap memory to store four stringsString javaStrings =mouse,cat,dog,car;/3.Allocate off-heap memory to store four pointersSegmentAllocator allocator=SegmentAllocator.implicitAllocator();MemorySegment offHeap =allocator.allocateArray(ValueLayout.ADDRESS,javaStrings.length);/4.Copy the strings from on-heap to off-heapfor(int i=0;i javaStrings.length;i+)/Allocate a string off-heap,then store a pointer to it MemorySegment cString=allocator.allocateUtf8String(javaStringsi);offHeap.setAtIndex(ValueLayout.ADDRESS,i,cString);/5.Sort the off-heap data by calling the foreign functionradixSort.invoke(offHeap,javaStrings.length,MemoryAddress.NULL,0);/6.Copy the(reordered)strings from off-heap to on-heapfor(int i=0;i executor.submit()-Thread.sleep(Duration.ofSeconds(1);return i;););/executor.close()is called implicitly,and waits如上使用了少数几个OS线程来运行10000个虚拟线程虚拟线程在超过上千个非CPU密集并发任务场景可以显著提升系统的吞吐率void handle(Request request,Response response)var url1=.var url2=.try(var executor=Executors.newVirtualThreadPerTaskExecutor()var future1=executor.submit()-fetchURL(url1);var future2=executor.submit()-fetchURL(url2);response.send(future1.get()+future2.get();catch(ExecutionException|InterruptedException e)response.fail(e);String fetchURL(URL url)throws IOException try(var in=url.openStream()return new String(in.readAllBytes(),StandardCharsets.UTF_8);像这种场景虽然是block的代码,但是因为引入的是虚拟线程,系统可以很好地伸缩;当虚拟线程block在IO或者其他操作()时,虚拟线程会从Thread BlockingQueue.take()unmount,当操作完成才重新mount上继续执行。不过有些操作不会unmount虚拟线程,会一同thread和底层的OS线程一起block住(比如进入synchronized代码块/方法,比如执行一个native方法或者foreign function)。虚拟线程开销不大,因而不需要使用池化技术使用可以以json格式来jcmd Thread.dump_to_file-format=json dump虚拟线程,实例如下Thread.Builder,Thread.ofVirtual(),Thread.ofPlatform()可以用来创建虚拟线程或者是平台线程,比如Thread thread=Thread.ofVirtual().name(duke).unstarted(runnable);Thread.startVirtualThread(Runnable)等同于创建和启动虚拟线程Thread.threadId()作为final方法会返回线程标识,而非final的Thread.getId()则被废弃Thread.getAllStackTraces()现在返回的是平台线程而非所有线程JEP 426:Vector API(Fourth Incubator)JEP 426:Vector API(Fourth Incubator)JDK16引入了提供了jdk.incubator.vector来用于矢量计JEP 338:Vector API(Incubator)算JDK17进行改进并作为第二轮的incubatorJEP 414:Vector API(Second Incubator)JDK18的进行改进并作为第三轮的incubator,而JEP 417:Vector API(Third Incubator)JDK19则作为第四轮的incubatorJEP 427:Pattern Matching for switch(Third Preview)JEP 427:Pattern Matching for switch(Third Preview)instanceof的模式匹配在JDK14作为preview,在JDK15作为第二轮的preview,在JDK16转正JDK17引入JEP 406:Pattern Matching for switch(Preview)JDK18的则作为第二轮的JEP 420:Pattern Matching for switch(Second Preview)preview,JDK19作为第三轮previewJEP 428:Structured Concurrency(Incubator)JEP 428:Structured Concurrency(Incubator)结构化并发也是JDK19的一个重要特性。JDK5引入的ExecutorService可以用于并行处理任务,比如R
展开阅读全文
提示  金锄头文库所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
关于本文
本文标题:Java程序高手进阶之Java19的新特性介绍
链接地址:https://www.jinchutou.com/shtml/view-337994252.html
关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.