From b8c3bf37e7e492f002eb1b83408297b76b030205 Mon Sep 17 00:00:00 2001 From: Miguel Reboiro-Jato Date: Sat, 26 Sep 2026 13:48:52 +0200 Subject: [PATCH] Adds virtual threads example --- pom.xml | 4 +- .../uvigo/esei/dai/threads/EchoBenchmark.java | 74 +++++++++++++++++++ .../example4/VirtualThreadEchoServer.java | 46 ++++++++++++ .../AsynchronousMultiThreadedEchoServer.java | 2 +- 4 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 src/main/java/es/uvigo/esei/dai/threads/EchoBenchmark.java create mode 100644 src/main/java/es/uvigo/esei/dai/threads/example4/VirtualThreadEchoServer.java rename src/main/java/es/uvigo/esei/dai/threads/{example4 => example5}/AsynchronousMultiThreadedEchoServer.java (98%) diff --git a/pom.xml b/pom.xml index e5e90a7..07960c1 100644 --- a/pom.xml +++ b/pom.xml @@ -32,8 +32,8 @@ - 11 - 11 + 21 + 21 UTF-8 UTF-8 diff --git a/src/main/java/es/uvigo/esei/dai/threads/EchoBenchmark.java b/src/main/java/es/uvigo/esei/dai/threads/EchoBenchmark.java new file mode 100644 index 0000000..6921b15 --- /dev/null +++ b/src/main/java/es/uvigo/esei/dai/threads/EchoBenchmark.java @@ -0,0 +1,74 @@ +/*- + * #%L + * Ejemplos de DAI - Multithilo + * %% + * Copyright (C) 2014 - 2026 Miguel Reboiro Jato + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * . + * #L% + */ +package es.uvigo.esei.dai.threads; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.InetAddress; +import java.net.Socket; +import java.net.UnknownHostException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +public class EchoBenchmark { + private final static String[] MESSAGES = { + "Hello", "How are you?", "I'm fine" + }; + + public static class EchoTask implements Runnable { + @Override + public void run() { + try (Socket socket = new Socket(InetAddress.getLocalHost(), 50000)) { + // Conexión con el servidor + final SocketIOManager ioManager = new SocketIOManager(socket); + + for (String message : MESSAGES) { + ioManager.println(message); + + ioManager.readLine(); + } + + ioManager.println("quit"); + } catch (final UnknownHostException e) { + System.out.println("Unknown host: localhost"); + } catch (final IOException e) { + System.out.println("Connection error: " + e.getMessage()); + } + } + } + + public static void main(String[] args) throws InterruptedException { + long millis = System.currentTimeMillis(); + ExecutorService service = Executors.newVirtualThreadPerTaskExecutor(); + + for (int i = 0; i < 10_000; i++) { + service.submit(new EchoTask()); + } + + service.shutdown(); + service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); + + System.out.println(System.currentTimeMillis() - millis); + } +} diff --git a/src/main/java/es/uvigo/esei/dai/threads/example4/VirtualThreadEchoServer.java b/src/main/java/es/uvigo/esei/dai/threads/example4/VirtualThreadEchoServer.java new file mode 100644 index 0000000..3a62533 --- /dev/null +++ b/src/main/java/es/uvigo/esei/dai/threads/example4/VirtualThreadEchoServer.java @@ -0,0 +1,46 @@ +/*- + * #%L + * Ejemplos de DAI - Multithilo + * %% + * Copyright (C) 2014 - 2023 Miguel Reboiro Jato + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * . + * #L% + */ +package es.uvigo.esei.dai.threads.example4; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import es.uvigo.esei.dai.threads.ServiceThread; + +public class VirtualThreadEchoServer { + public static void main(String[] args) { + try (ServerSocket serverSocket = new ServerSocket(50000)) { + final ExecutorService threadPool = Executors.newVirtualThreadPerTaskExecutor(); + + while (true) { + final Socket clientSocket = serverSocket.accept(); + + threadPool.execute(new ServiceThread(clientSocket)); + } + } catch (final IOException e) { + System.err.println("Server socket could not be created"); + } + } +} diff --git a/src/main/java/es/uvigo/esei/dai/threads/example4/AsynchronousMultiThreadedEchoServer.java b/src/main/java/es/uvigo/esei/dai/threads/example5/AsynchronousMultiThreadedEchoServer.java similarity index 98% rename from src/main/java/es/uvigo/esei/dai/threads/example4/AsynchronousMultiThreadedEchoServer.java rename to src/main/java/es/uvigo/esei/dai/threads/example5/AsynchronousMultiThreadedEchoServer.java index 35b992b..a5b2cef 100644 --- a/src/main/java/es/uvigo/esei/dai/threads/example4/AsynchronousMultiThreadedEchoServer.java +++ b/src/main/java/es/uvigo/esei/dai/threads/example5/AsynchronousMultiThreadedEchoServer.java @@ -19,7 +19,7 @@ * . * #L% */ -package es.uvigo.esei.dai.threads.example4; +package es.uvigo.esei.dai.threads.example5; import java.io.IOException; import java.net.InetSocketAddress; -- 2.18.1