Технологии Java
Неблокирующий и асинхронный ввод-вывод
while (!Thread.interrupted()) { selector.select(); for (final Iterator<SelectionKey> i = selector.selectedKeys().iterator(); i.hasNext(); ) { final SelectionKey key = i.next(); try { if (key.isAcceptable()) { final ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel(); handle(serverChannel.accept()); } } finally { i.remove(); } } }
while (!Thread.interrupted()) { selector.select(key -> { try { if (key.isAcceptable()) { final ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel(); handle(serverChannel.accept()); } } catch (final IOException e) { throw new UncheckedIOException(e); } }); }
socketChannel.read( buffer, context, new CompletionHandler<Integer, Context>() { public void completed(Integer result, Context context) { if (context.process(result)) { socketChannel.read(buffer, this); } } public void failed(Throwable e, Context context) { context.error(e); } } );
try ( ExecutorService executor = Executors .newVirtualThreadPerTaskExecutor() ) { IntStream.range(0, 10_000).forEach(i -> { executor.submit(() -> { Thread.sleep(Duration.ofSeconds(1)); return i; }); }); }
// + response.send( future1.get() // ‒ + // + future2.get() // ‒ ); // +