Commit b8c3bf37 authored by Miguel Reboiro-Jato's avatar Miguel Reboiro-Jato

Adds virtual threads example

parent 29d4a024
......@@ -32,8 +32,8 @@
</licenses>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
......
/*-
* #%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
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #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);
}
}
/*-
* #%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
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #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");
}
}
}
......@@ -19,7 +19,7 @@
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
package es.uvigo.esei.dai.threads.example4;
package es.uvigo.esei.dai.threads.example5;
import java.io.IOException;
import java.net.InetSocketAddress;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment