I am using Socket.IO-java on the desktop application to handle realtime communication with the server. I was wondering if I could get some feed back on the setup as I am not happy with how it's done, but I can't come up with a better solution.There exists a Main thread (from main(..) method) that launches a Daemon thread (DT). Daemon thread calls a method that creates SocketIO object and installs its callbacks. I show one here for brevity.public void websocketConnectionSetup(...) {        Executor executor = Executors.newSingleThreadExecutor();        Socket socketIOSocket = null;        try {            IO.Options options = new IO.Options();            socketIOSocket = IO.socket(hostname, options);        } catch (URISyntaxException e) {            Logger.error(e);        }        socketIOSocket.on(                WSEvents.ON_DEMAND_SYNC, // WSEvents class just contains static strings                new WSHandler<>(WSEvents.ON_DEMAND_SYNC, socketIOSocket, executor));        socketIOSocket.connect();}WSHandler implements Emitter.Listener which is required by Socket.IO to create a callback. WSHandler has a single method, which is basically a big if statement that switches on websocket events:@Overridepublic void call(final Object... objects) {    if (event.equals(WSEvents.PING)) {        executor.execute(new WSSimpleWorker<>(event, socketIOSocket, null, getParameterClass()));    ...    } else if (event.equals(WSEvents.ON_DEMAND_SYNC)) {        executor.execute(new WSWorker(args));    }    ...}As you can see I create a thread to handle the event. In the case of the on-demand sync, I create a task that pushes some data back to the server. This is the question: I want to make sure that there is only one Executor for these websocket workers. That's why I create it in the Daemon thread and then pass it down to WSWorker threads. However, that seems wrong because I pass the same object to different threads. I have other shared objects (for db connection and login config), and I use a synchronized Singleton to make sure they are thread safe. However, I don't understand how to ensure that I have one Executor and that's its thread safe. Or does the framework ensure it?I also need to be able to shutdown this Executor when the Daemon thread exits, so I think I need to make this object accessible from anywhere in the application. The only solution I can think of is to wrap it into a singleton class to ensure that only one Executor is created. But in that case I am solving all synchronization problems with singletons..
How to ensure there is one Executor
java;multithreading;thread safety;socket.io