简体   繁体   中英

How to synchronize the pass of two queues to thread constructor?

I'm trying to deal with synchronization of two queues passed to two threads in cascading way. So i have two threads, the ServerThread that listens to the connections from clients, and the ConnectedThread, that is created in case of the accepted connection from the client by server thread. I do not know how to synchronize the pass of these queues to the thread constructor of ConnectedThread class. Should I synchronize these queues or not? And I do not know what is the best practice. I tried to do this by:

synchronized (this) {
    new ConnectedThread(socket, inQueue, outQueue).start();
}

and by:

synchronized (inQueue) {
    synchronized (outQueue) {
        new ConnectedThread(socket, inQueue, outQueue).start();
    }
}

Everything is of course in the "run()" method of thread of ServerThread class.

Everything in code looks like this:

private class ServerThread extends Thread {
    public ServerThread(int port, Queue<ThreadMessage> in,
            Queue<ThreadMessage> out) throws Exception {
        // TODO Auto-generated constructor stub

        setName("ServerThread");

        try {
             server = new ServerSocket(port);

             synchronized (in) {
                 synchronized (out) {
                     inQueue = in;   outQueue = out;
                 }
             }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            throw new Exception();
        }
   }

   private ServerSocket server;

   private Queue<ThreadMessage> inQueue, outQueue;

   private boolean running = true;

   @Override
   public void run() {
       // TODO Auto-generated method stub
       while (running) {
           try {
               Socket socket = server.accept();

               synchronized (inQueue) {
                   synchronized (outQueue) {

                       try {
                           new ConnectedThread(socket, inQueue, outQueue)
                           .start();
                       } catch (Exception e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                       }

                   }
               }

           } catch (IOException e) {
           // TODO Auto-generated catch block
               e.printStackTrace();
           }
       }
   }
}

The thread of SeverThread class is created inside synchronized method.

public synchronized void startServer(int port, Queue<ThreadMessage> in, 
        Queue<ThreadMessage> out) throws Exception {

    serverThread = new ServerThread(port, in, out);
    serverThread.start();
}

Any suggestions how to deal with it in a proper way?

Thank in advance.

PS Sorry for my english. PS The queues are instances of LinkedList

You do not need to synchronize the assignment of the references of Queue instances to class members.

What you have to synchronize is the operations you perform on queues, like poll() or offer() .

What is synchronization good for? Well, when there are multiple threads (in your example ConnectedThread ) that work with a shared object (in your example the in and out queues) you must synchronize operations in order for application to execute operations in thread-safe way.

Example: if two threads wants to read from a queue, and both of them see that the queue is not empty (the queue contains one element), they both will call poll on it, but only one thread will get the element from the queue. The other thread will get null and cause problems in your logic. For that reason, you must synchronize reading and writing operations for a queue.

The synchronization is only useful when accessing the object (For example, when browsing or modifying your queue), not the reference (ie assignement or argument passing).

Your posted code doesn't mention how the queues are handled.

还考虑使用Java SE API中存在的并发队列实现:“ 队列实现”教程页面

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