From ae82631050eb517114379995fabfe0ee741a349f Mon Sep 17 00:00:00 2001 From: Andrew Hatch Date: Sun, 6 Oct 2019 22:06:36 +0100 Subject: [PATCH] use Executor services for thread pooling/management; some tidy up --- .../java/io/github/chronosx88/JGUN/Dispatcher.java | 11 +++++++---- src/main/java/io/github/chronosx88/JGUN/Dup.java | 2 -- .../chronosx88/JGUN/examples/MainClientServer.java | 8 ++++++-- .../JGUN/futures/BaseCompletableFuture.java | 4 ++-- .../io/github/chronosx88/JGUN/nodes/GunClient.java | 4 +--- .../io/github/chronosx88/JGUN/nodes/GunSuperPeer.java | 3 +-- .../JGUN/storageBackends/InMemoryGraph.java | 9 +++++++-- 7 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/main/java/io/github/chronosx88/JGUN/Dispatcher.java b/src/main/java/io/github/chronosx88/JGUN/Dispatcher.java index 92b41a1..ec6a697 100644 --- a/src/main/java/io/github/chronosx88/JGUN/Dispatcher.java +++ b/src/main/java/io/github/chronosx88/JGUN/Dispatcher.java @@ -12,6 +12,8 @@ import org.json.JSONObject; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; public class Dispatcher { private final Map> pendingFutures = new ConcurrentHashMap<>(); @@ -20,6 +22,7 @@ public class Dispatcher { private final Peer peer; private final StorageBackend graphStorage; private final Dup dup; + private final Executor executorService = Executors.newCachedThreadPool(); public Dispatcher(StorageBackend graphStorage, Peer peer, Dup dup) { this.graphStorage = graphStorage; @@ -83,17 +86,17 @@ public class Dispatcher { } public void sendPutRequest(String messageID, JSONObject data) { - new Thread(() -> { + executorService.execute(() -> { InMemoryGraph graph = Utils.prepareDataForPut(data); peer.emit(Utils.formatPutRequest(messageID, graph.toJSONObject()).toString()); - }).start(); + }); } public void sendGetRequest(String messageID, String key, String field) { - new Thread(() -> { + executorService.execute(() -> { JSONObject jsonGet = Utils.formatGetRequest(messageID, key, field); peer.emit(jsonGet.toString()); - }).start(); + }); } public void addChangeListener(String soul, NodeChangeListener listener) { diff --git a/src/main/java/io/github/chronosx88/JGUN/Dup.java b/src/main/java/io/github/chronosx88/JGUN/Dup.java index 6e19635..874579f 100644 --- a/src/main/java/io/github/chronosx88/JGUN/Dup.java +++ b/src/main/java/io/github/chronosx88/JGUN/Dup.java @@ -1,13 +1,11 @@ package io.github.chronosx88.JGUN; import java.util.Map; -import java.util.Random; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; public class Dup { private static char[] randomSeed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); - private static Random random = new Random(System.currentTimeMillis()); private Map s = new ConcurrentHashMap<>(); private DupOpt opt = new DupOpt(); private Thread to = null; diff --git a/src/main/java/io/github/chronosx88/JGUN/examples/MainClientServer.java b/src/main/java/io/github/chronosx88/JGUN/examples/MainClientServer.java index d5bb75a..aa4bd7c 100644 --- a/src/main/java/io/github/chronosx88/JGUN/examples/MainClientServer.java +++ b/src/main/java/io/github/chronosx88/JGUN/examples/MainClientServer.java @@ -9,12 +9,14 @@ import org.json.JSONObject; import java.net.Inet4Address; import java.net.UnknownHostException; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; public class MainClientServer { public static void main(String[] args) { GunSuperPeer gunSuperNode = new GunSuperPeer(21334, new InMemoryGraph()); gunSuperNode.start(); - new Thread(() -> { + Runnable task = () -> { Gun gun = null; try { gun = new Gun(Inet4Address.getByAddress(new byte[]{127, 0, 0, 1}), 21334, new InMemoryGraph()); @@ -45,7 +47,9 @@ public class MainClientServer { System.out.println("Deleting an item random/dVFtzE9CL"); gun.get("random").get("dVFtzE9CL").put(null).await(); gun.get("random").put(new JSONObject().put("hello", "world")).await(); - }).start(); + }; + Executor executorService = Executors.newSingleThreadExecutor(); + executorService.execute(task); } } diff --git a/src/main/java/io/github/chronosx88/JGUN/futures/BaseCompletableFuture.java b/src/main/java/io/github/chronosx88/JGUN/futures/BaseCompletableFuture.java index 55b8363..f624d9a 100644 --- a/src/main/java/io/github/chronosx88/JGUN/futures/BaseCompletableFuture.java +++ b/src/main/java/io/github/chronosx88/JGUN/futures/BaseCompletableFuture.java @@ -1,9 +1,9 @@ package io.github.chronosx88.JGUN.futures; -import java9.util.concurrent.CompletableFuture; - import java.util.concurrent.ExecutionException; +import java9.util.concurrent.CompletableFuture; + public class BaseCompletableFuture extends CompletableFuture { private final String futureID; diff --git a/src/main/java/io/github/chronosx88/JGUN/nodes/GunClient.java b/src/main/java/io/github/chronosx88/JGUN/nodes/GunClient.java index 53a1901..c1c049c 100644 --- a/src/main/java/io/github/chronosx88/JGUN/nodes/GunClient.java +++ b/src/main/java/io/github/chronosx88/JGUN/nodes/GunClient.java @@ -2,8 +2,8 @@ package io.github.chronosx88.JGUN.nodes; import io.github.chronosx88.JGUN.Dispatcher; import io.github.chronosx88.JGUN.Dup; -import io.github.chronosx88.JGUN.PathRef; import io.github.chronosx88.JGUN.storageBackends.StorageBackend; + import org.java_websocket.client.WebSocketClient; import org.java_websocket.handshake.ServerHandshake; import org.json.JSONObject; @@ -14,12 +14,10 @@ import java.net.URISyntaxException; public class GunClient extends WebSocketClient implements Peer { private Dup dup = new Dup(); - private final StorageBackend storage; private final Dispatcher dispatcher; public GunClient(InetAddress address, int port, StorageBackend storage) throws URISyntaxException { super(new URI("ws://" + address.getHostAddress() + ":" + port)); - this.storage = storage; this.dispatcher = new Dispatcher(storage, this, dup); } diff --git a/src/main/java/io/github/chronosx88/JGUN/nodes/GunSuperPeer.java b/src/main/java/io/github/chronosx88/JGUN/nodes/GunSuperPeer.java index f685e9e..afd78b3 100644 --- a/src/main/java/io/github/chronosx88/JGUN/nodes/GunSuperPeer.java +++ b/src/main/java/io/github/chronosx88/JGUN/nodes/GunSuperPeer.java @@ -3,6 +3,7 @@ package io.github.chronosx88.JGUN.nodes; import io.github.chronosx88.JGUN.Dispatcher; import io.github.chronosx88.JGUN.Dup; import io.github.chronosx88.JGUN.storageBackends.StorageBackend; + import org.java_websocket.WebSocket; import org.java_websocket.handshake.ClientHandshake; import org.java_websocket.server.WebSocketServer; @@ -12,14 +13,12 @@ import java.net.InetSocketAddress; public class GunSuperPeer extends WebSocketServer implements Peer { private Dup dup = new Dup(); - private StorageBackend graph; private Dispatcher dispatcher; public GunSuperPeer(int port, StorageBackend storageBackend) { super(new InetSocketAddress(port)); setReuseAddr(true); dispatcher = new Dispatcher(storageBackend, this, dup); - this.graph = storageBackend; } @Override diff --git a/src/main/java/io/github/chronosx88/JGUN/storageBackends/InMemoryGraph.java b/src/main/java/io/github/chronosx88/JGUN/storageBackends/InMemoryGraph.java index 4cd78e3..f8bee80 100644 --- a/src/main/java/io/github/chronosx88/JGUN/storageBackends/InMemoryGraph.java +++ b/src/main/java/io/github/chronosx88/JGUN/storageBackends/InMemoryGraph.java @@ -1,9 +1,14 @@ package io.github.chronosx88.JGUN.storageBackends; import io.github.chronosx88.JGUN.Node; + import org.json.JSONObject; -import java.util.*; +import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; public class InMemoryGraph implements StorageBackend { @@ -74,4 +79,4 @@ public class InMemoryGraph implements StorageBackend { public boolean isEmpty() { return nodes.isEmpty(); } -} \ No newline at end of file +}