diff --git a/pom.xml b/pom.xml index e5e90a7649db82cbd2fefce1dbd612c94eacdc2d..07960c14ea3d665aeb3adf15bcbd183dfe6cb8c6 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 0000000000000000000000000000000000000000..6921b159248380f933d88bdf0270edc005d4ea37 --- /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 0000000000000000000000000000000000000000..3a6253345d93f3f9c2001287bb25b148c413bb0d --- /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 35b992bc19f0108b0283a82710885435374f2248..a5b2cef1a3dc8b8fa2370f4a6a6a9d302074c89d 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;