From 5074026c95ecf42f76ffcd1355c57b90e987a908 Mon Sep 17 00:00:00 2001 From: "Josiah (Gaming32) Glosson" Date: Sun, 12 May 2024 15:53:43 -0500 Subject: [PATCH] Use virtual threads for UPnP search (90 threads on my system) --- .../gaming32/worldhost/upnp/GatewayFinder.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/github/gaming32/worldhost/upnp/GatewayFinder.java b/src/main/java/io/github/gaming32/worldhost/upnp/GatewayFinder.java index 03b3d98..db73c01 100644 --- a/src/main/java/io/github/gaming32/worldhost/upnp/GatewayFinder.java +++ b/src/main/java/io/github/gaming32/worldhost/upnp/GatewayFinder.java @@ -41,13 +41,12 @@ public class GatewayFinder { SEARCH_MESSAGES = m.toArray(new String[]{}); } - private class GatewayListener extends Thread { + private class GatewayListener implements Runnable { private final InetAddress ip; private final String req; public GatewayListener(InetAddress ip, String req) { - setName("UPnP Gateway Finder " + ip); this.ip = ip; this.req = req; } @@ -86,7 +85,7 @@ public void run() { } } - private final LinkedList listeners = new LinkedList<>(); + private final LinkedList listeners = new LinkedList<>(); private final Consumer onFound; public GatewayFinder(Consumer onFound) { @@ -94,14 +93,16 @@ public GatewayFinder(Consumer onFound) { for (InetAddress ip : getLocalIPs()) { for (String req : SEARCH_MESSAGES) { GatewayListener l = new GatewayListener(ip, req); - l.start(); - listeners.add(l); + final Thread thread = Thread.ofVirtual() + .name("UPnP Gateway Finder " + ip) + .start(l); + listeners.add(thread); } } } public boolean isSearching() { - for (GatewayListener l : listeners) { + for (Thread l : listeners) { if (l.isAlive()) { return true; }