Skip to content

Commit

Permalink
Use virtual threads for UPnP search (90 threads on my system)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed May 12, 2024
1 parent 3f28b2e commit 5074026
Showing 1 changed file with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -86,22 +85,24 @@ public void run() {
}
}

private final LinkedList<GatewayListener> listeners = new LinkedList<>();
private final LinkedList<Thread> listeners = new LinkedList<>();
private final Consumer<Gateway> onFound;

public GatewayFinder(Consumer<Gateway> onFound) {
this.onFound = 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;
}
Expand Down

0 comments on commit 5074026

Please sign in to comment.