Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7854269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:00:13+00:00 2026-06-02T20:00:13+00:00

I’m working on a project in which I have a class, DeviceCommunicator , that

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:00:14+00:00Added an answer on June 2, 2026 at 8:00 pm

    If you have control over the DeviceCommunicator class, I would pass in the Stream or Reader objects that you need as arguments to the constructor. I certainly would not use static variables in that manner.

    new Thread(new DeviceCommunicator(hostName, portNum, in, out)).start();
    

    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 Queue of messages to send. That also could be an argument to the DeviceCommunicator although you will need to make that a synchronized list or something:

    List<String> toSendList = Collections.synchronizedList(new ArrayList<String>());
    ...
    new Thread(new DeviceCommunicator(hostName, portNum, in, out, toSendList)).start();
    

    However, a better pattern would be to add a send method on the DeviceCommunicator that 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 to DeviceCommunicator, 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.