Skip to content

Commit d4eeff7

Browse files
committed
Add scheduling
1 parent eaa2f7e commit d4eeff7

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

src/main/java/com/lbry/globe/thread/DHTNodeFinderThread.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.net.*;
1212
import java.util.*;
1313
import java.util.concurrent.ConcurrentLinkedQueue;
14+
import java.util.concurrent.Executors;
15+
import java.util.concurrent.TimeUnit;
1416

1517
import org.json.JSONObject;
1618

@@ -43,29 +45,22 @@ public void run(){
4345
}
4446

4547
private void startSender(){
46-
new Thread(() -> {
47-
while(true){
48-
System.out.println("[BULK PING]");
49-
for(InetSocketAddress socketAddress : DHT.getPeers().keySet()){
50-
String hostname = socketAddress.getHostName();
51-
int port = socketAddress.getPort();
52-
try{
53-
for(InetAddress ip : InetAddress.getAllByName(hostname)){
54-
InetSocketAddress destination = new InetSocketAddress(ip,port);
55-
this.doPing(destination);
56-
}
57-
}catch(Exception e){
58-
e.printStackTrace();
48+
Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(() -> {
49+
System.out.println("[BULK PING]");
50+
API.saveNodes();
51+
for(InetSocketAddress socketAddress : DHT.getPeers().keySet()){
52+
String hostname = socketAddress.getHostName();
53+
int port = socketAddress.getPort();
54+
try{
55+
for(InetAddress ip : InetAddress.getAllByName(hostname)){
56+
InetSocketAddress destination = new InetSocketAddress(ip,port);
57+
this.doPing(destination);
5958
}
59+
}catch(Exception e){
60+
e.printStackTrace();
6061
}
61-
try {
62-
Thread.sleep(15_000);
63-
} catch (InterruptedException e) {
64-
throw new RuntimeException(e);
65-
}
66-
API.saveNodes();
6762
}
68-
}).start();
63+
},0,15,TimeUnit.SECONDS);
6964
}
7065

7166
private void doPing(InetSocketAddress destination) throws IOException{

0 commit comments

Comments
 (0)