From d4eeff7488d6fb2bef2650e14f41188c9e21cf25 Mon Sep 17 00:00:00 2001 From: Ben van Hartingsveldt Date: Sat, 12 Jul 2025 19:23:40 +0200 Subject: [PATCH] Add scheduling --- .../globe/thread/DHTNodeFinderThread.java | 35 ++++++++----------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/lbry/globe/thread/DHTNodeFinderThread.java b/src/main/java/com/lbry/globe/thread/DHTNodeFinderThread.java index 24ea16a..f02c664 100644 --- a/src/main/java/com/lbry/globe/thread/DHTNodeFinderThread.java +++ b/src/main/java/com/lbry/globe/thread/DHTNodeFinderThread.java @@ -11,6 +11,8 @@ import java.io.IOException; import java.net.*; import java.util.*; import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; import org.json.JSONObject; @@ -43,29 +45,22 @@ public class DHTNodeFinderThread implements Runnable{ } private void startSender(){ - new Thread(() -> { - while(true){ - System.out.println("[BULK PING]"); - for(InetSocketAddress socketAddress : DHT.getPeers().keySet()){ - String hostname = socketAddress.getHostName(); - int port = socketAddress.getPort(); - try{ - for(InetAddress ip : InetAddress.getAllByName(hostname)){ - InetSocketAddress destination = new InetSocketAddress(ip,port); - this.doPing(destination); - } - }catch(Exception e){ - e.printStackTrace(); + Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(() -> { + System.out.println("[BULK PING]"); + API.saveNodes(); + for(InetSocketAddress socketAddress : DHT.getPeers().keySet()){ + String hostname = socketAddress.getHostName(); + int port = socketAddress.getPort(); + try{ + for(InetAddress ip : InetAddress.getAllByName(hostname)){ + InetSocketAddress destination = new InetSocketAddress(ip,port); + this.doPing(destination); } + }catch(Exception e){ + e.printStackTrace(); } - try { - Thread.sleep(15_000); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - API.saveNodes(); } - }).start(); + },0,15,TimeUnit.SECONDS); } private void doPing(InetSocketAddress destination) throws IOException{