|
11 | 11 | import java.net.*; |
12 | 12 | import java.util.*; |
13 | 13 | import java.util.concurrent.ConcurrentLinkedQueue; |
| 14 | +import java.util.concurrent.Executors; |
| 15 | +import java.util.concurrent.TimeUnit; |
14 | 16 |
|
15 | 17 | import org.json.JSONObject; |
16 | 18 |
|
@@ -43,29 +45,22 @@ public void run(){ |
43 | 45 | } |
44 | 46 |
|
45 | 47 | 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); |
59 | 58 | } |
| 59 | + }catch(Exception e){ |
| 60 | + e.printStackTrace(); |
60 | 61 | } |
61 | | - try { |
62 | | - Thread.sleep(15_000); |
63 | | - } catch (InterruptedException e) { |
64 | | - throw new RuntimeException(e); |
65 | | - } |
66 | | - API.saveNodes(); |
67 | 62 | } |
68 | | - }).start(); |
| 63 | + },0,15,TimeUnit.SECONDS); |
69 | 64 | } |
70 | 65 |
|
71 | 66 | private void doPing(InetSocketAddress destination) throws IOException{ |
|
0 commit comments