This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| record Employee(Long id, String name, String department) { }; | |
| record Contractor(Long id, String name, BigDecimal fee) { }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package nl.rockstars.loom; | |
| public class VirtualThread { | |
| Runnable runnable = () -> { | |
| System.out.println(", I am a virtual thread: " + Thread.currentThread().isVirtual()); | |
| }; | |
| public static void main(String... args) { | |
| new VirtualThread().run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CustomerInfo retrieveCustomerInfo(Long id) throws ExecutionException, InterruptedException { | |
| try (var scope = new StructuredTaskScope.ShutdownOnFailure()()) { | |
| Future<Customer> customerFuture = scope.fork(() -> retrieveCustomer(id)); | |
| Future<Orders> ordersFuture = scope.fork(() -> retrieveCustomerOrders(id)); | |
| Future<CustomerSalesHistory> historyFuture = scope.fork(() -> retrieveCustomerHistory(id)); | |
| scope.join(); | |
| scope.throwIfFailed(e -> new WebApplicationException(e)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CustomerInfo retrieveCustomerInfo(Long id) throws ExecutionException, InterruptedException { | |
| try (var pool = new ScheduledThreadPoolExecutor()) { | |
| Future<Customer> customerFuture = pool.submit(() -> retrieveCustomer(id)); | |
| Future<CustomerOrders> ordersFuture = pool.submit(() -> retrieveCurrentCustomerOrders(id)); | |
| Future<CustomerSalesHistory> historyFuture = pool.submit(() -> retrieveCustomerHistory(id)); | |
| Customer customer = customerFuture.get(); | |
| Collection<Orders> outstandingOrders = ordersFuture.get(); | |
| Collection<CustomerSalesHistory> salesHistory = historyFuture.get(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private String printValue(Object obj) { | |
| return switch (obj) { | |
| case String s when s.length() > 10 -> String.format("Long String with value %s", s); | |
| case String s -> String.format("Not so long String with value %s", s); | |
| default -> String.format("Dunno the type, but the value is %s", obj.toString()); | |
| }; | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private String printValue(Object obj) { | |
| return switch (obj) { | |
| case null -> throw new NullPointerException(); | |
| case String s when s.length() > 10 -> String.format("Long String with value %s", s); | |
| case String s -> String.format("Not so long String with value %s", s); | |
| default -> String.format("Dunno the type, but the value is %s", obj.toString()); | |
| }; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private String printValue(Object obj) { | |
| return switch (obj) { | |
| case null -> new String("You can't pass in a null value!"); | |
| case String s when s.length() > 10 -> String.format("Long String with value %s", s); | |
| case String s -> String.format("Not so long String with value %s", s); | |
| default -> String.format("Dunno the type, but the value is %s", obj.toString()); | |
| }; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private void process(Object o) { | |
| if (o instanceof Employee(String id, String name, String department)) { | |
| System.out.println(name + " => " + department); | |
| } | |
| if (o instanceof Contractor(String id, String name, BigDecimal fee)) { | |
| System.out.println(name + " => " + fee.toPlainString()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.math.BigDecimal; | |
| public class RecordPatternMatchingClassic { | |
| public static void main(String...args) { | |
| new RecordPatternMatchingClassic().execute(); | |
| } | |
| private void execute() { | |
| var john = new Employee("12", "John", "Auditing"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | |
| */ | |
| package kotlinx.coroutines.guide.exampleCompose02 | |
| import kotlinx.coroutines.* | |
| import kotlin.system.* | |
| fun main() = runBlocking<Unit> { | |
| val time = measureTimeMillis { |
NewerOlder