I’m working on a project in which I have a class, DeviceCommunicator, that implements Runnable. Currently, a main class instantiates a single instance of DeviceCommunicator which (eventually) connects to a device on the local network using the Socket library.
By eventually, I mean that if a message needs to be sent, then the instance of the DeviceCommunicator opens the socket connection to the device, sends the message, and then starts a new thread to receive the data back from the socket via the following line of code:
new Thread(new DeviceCommunicator()).start();
EDIT: To clarify, this is the order of operations when the program is executed:
1) MAIN class instantiates a DeviceCommunicator class w/ constructor like:
comm1 = DeviceCommunicator(hostName, portNum)
2) MAIN class wants to send a message to comm1, so it calls send like:
comm1.send(someString)
3) comm1 is of type DeviceCommunicator and opens a Socket connection to hostName/portNum like:
deviceSocket = new Socket(hostName, portNum);
out = new PrintStream(deviceSocket.getOutputStream());
in = new BufferedReader(new InputStreamReader(deviceSocket.getInputStream()));
4) comm1 sends someStr to the output PrintStream and then initializes a thread to listen for a response with the following code:
new Thread(new DeviceCommunicator()).start();
Due to the fact that the listening DeviceCommunicator thread has no constructor arguments, it was required that I make the output PrintStream and the input BufferedReader static variables.
When I have only one instance of DeviceCommunicator – this works great!
However, I would like multiple instances of the DeviceCommunicator class that can connect to either the same or a different device on the local network, BUT considering the fact that the output and input in the DeviceCommunicator class are static, then they are shared (I think, I’ve read that the JVM doesn’t quite guarantee that static variable changes will be made visible to other running threads) across all instances of DeviceCommunicator – this is a problem!
I’ve done some research and I haven’t come across quite a similar topic – most topics are basically an “either-or” where:
A) The topic is about threaded socket communication, wherein “non-blocking” communication is accomplished by the use of static variables.
or
B) A simple implements Runnable case is considered wherein one thread will accomplish one (typically simple) task while the other accomplishes another (typically slightly modified) task.
EDIT: I guess one solution that might be proposed is to simply pass the input BufferedReader to the listening DeviceCommunicator thread, however I am implementing a queue of messages that are to be sent (in case there are network problems); so, if a message needs to be sent, it simply gets the first element in the queue and prints it to the socket connection, and I would like to, in the listening thread, verify that the message was correctly received by the device. If the message was correctly received, then I would like to remove the element from the queue, but this again poses a problem – passing variables in Java is always by value, not by reference! So, if I was to pass the input BufferedReader and the queue, the queue that was being modified in the listening DeviceCommunicator would not be the actual queue that needed to be modified in the main DeviceCommunicator instance.
Is there an obvious solution to this problem that I am unaware of?
Thanks in advance!
If you have control over the
DeviceCommunicatorclass, I would pass in theStreamorReaderobjects that you need as arguments to the constructor. I certainly would not usestaticvariables in that manner.Now, if multiple threads are reading and writing out the streams then you will have to synchronize on them before doing that.
You also mentioned in your comments that you need to have a
Queueof messages to send. That also could be an argument to theDeviceCommunicatoralthough you will need to make that a synchronized list or something:However, a better pattern would be to add a
sendmethod on theDeviceCommunicatorthat would add the items to the queue instead of both the main thread and the sending thread having the same queue. If you add methods toDeviceCommunicator, you might be able to have it also hide the reader and writer streams as well and the main thread would not access the streams directly. Data hiding is one of the important features of object oriented programs.