Virtual Threads with Project Loom:
Use try-with-resources on Executors.newVirtualThreadPerTaskExecutor. Call IntStream.range from 0 to 10000 and forEach to submit tasks that sleep for one second and return the iteration value.
Structured Concurrency Preview Pattern:
Use try-with-resources on new StructuredTaskScope.ShutdownOnFailure. Fork tasks for fetching user and orders by calling scope.fork with lambda expressions. Call scope.join then throwIfFailed. Return new composite object with results from both task suppliers.
Pattern Matching for Switch:
Create describe method taking Object parameter. Use switch expression with cases for Integer i with guard condition i greater than 0 returning positive integer message, Integer i returning non-positive message, String s returning length message, List with wildcard returning size message, null returning null value, and default returning unknown type.
Record Patterns and Sealed Classes:
Define Point record with int x and int y. Define Rectangle record with Point topLeft and Point bottomRight. Create area method that uses switch with Rectangle pattern destructuring both Point components into variables, returning absolute value of width times height. Define sealed Shape interface permitting Circle and Rectangle. Implement Circle record with area method using PI times radius squared.
基于Project Loom的Virtual Threads:
在Executors.newVirtualThreadPerTaskExecutor上使用try-with-resources语法。调用IntStream.range(0,10000)并通过forEach提交休眠1秒后返回迭代值的任务。
结构化并发预览模式:
在new StructuredTaskScope.ShutdownOnFailure上使用try-with-resources语法。通过调用scope.fork并传入lambda表达式,分叉获取用户和订单的任务。调用scope.join后再调用throwIfFailed。返回包含两个任务结果的新复合对象。
Switch语句的模式匹配:
创建一个接收Object参数的describe方法。使用switch表达式,包含以下分支:带有守卫条件i>0的Integer i分支,返回正整数消息;Integer i分支,返回非正整数消息;String s分支,返回长度消息;带通配符的List分支,返回大小消息;null分支,返回空值消息;default分支,返回未知类型消息。
记录模式与密封类:
定义包含int x和int y的Point记录。定义包含Point topLeft和Point bottomRight的Rectangle记录。创建area方法,使用switch语句结合Rectangle模式,解构两个Point组件到变量中,返回宽度乘以高度的绝对值。定义密封的Shape接口,允许Circle和Rectangle实现。实现带有area方法的Circle记录,使用PI乘以半径的平方计算面积。