简体   繁体   中英

Java - can two threads on client side use the same input stream from server?

I'm working on a Java client/server application with a pretty specific set of rules as to how I have to develop it. The server creates a ClientHandler instance that has input and output streams to the client socket, and any input and output between them is triggered by events in the client GUI.

I have now added in functionality server-side that will send out periodic updates to all connected clients (done by storing each created PrintWriter object from the ClientHandlers in an ArrayList<PrintWriter> ). I need an equivalent mechanism client-side to process these messages, and have been told this needs to happen in a second client-side thread whose run() method uses a do...while(true) loop until the client disconnects.

This all makes sense to me so far, what I am struggling with is the fact that the two threads will have to share the one input stream, and essentially 'ignore' any messages that aren't of the type that they handle. In my head, it should look something like this:

Assuming that every message from server sends a boolean of value true on a message-to-all, and one of value false on a message to an individual client...

Existing Client Thread
//method called from actionPerformed(ActionEvent e)
//handles server response to bid request
public void receiveResponse()
{
    //thread should only process to-specific-client messages
    if (networkInput.nextBoolean() == false)
    {
        //process server response...
    }
}


Second Client-side Thread
//should handle all messages set to all clients
run()
{
    do {
        if (networkInput.nextBoolean() == true)
        {
            //process broadcasted message...
        } while (true);
}

As they need to use the same input stream, I would obviously be adding some synchronized , wait/notify calls, but generally, is what I'm looking to do here possible? Or will the two threads trying to read in from the same input stream interfere with each other too much?

Please let me know what you think!

Thanks, Mark

You can do it, though it will be complicated to test and get right. How much is "too much" depends on you. A simpler solution is to have a reader thread pass messages to the two worker threads.

ExecutorService thread1 = Executors.newSingleThreadedExecutors();
ExecutorService thread2 = Executors.newSingleThreadedExecutors();
while(running) {
    Message message = input.readMessage();
    if (message.isTypeOne())
        thread1.submit(() -> process(message));
    else if (message.isTypeTwo())
        thread2.submit(() -> process(message));
    else
        // do something else.
}
thread1.shutdown();
thread2.shutdown();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM