Can not access object's properties(methods) fr

2019-03-04 03:10发布

问题:

I have the following code in ServerConnectionManager:

public class ServerConnectionManager implements Runnable {

    private DatagramPacket receivedPacket;
    //some more things here


    public ServerConnectionManager(DatagramPacket receivedPacket){
        this.receivedPacket = receivedPacket;


        System.out.println("Connection manager has been assigned a request");
        System.out.println("The port of the request packet is "+receivedPacket.getPort());
         try {
             sendReceiveSocket = new DatagramSocket();
          } catch (SocketException se) {  
             se.printStackTrace();
             System.exit(1);
          }


    }

    @Override
    public void run() {

        //DEBUGGING LINES HERE
          System.out.println("The start method on connection manager works..");
          System.out.println("Point A");
          System.out.println("The port of the request packet is "+receivedPacket.getPort()); // the thread gets stuck here
          System.out.println("Does this work..?"); //This line never gets printed

          //some other stuff to be done here
                    }


         }

And i have some code in the run method of some other threads that make use of ServerConnectionManager: Lets Call this Thread B

    @Override
    public void run() {

        while(true){
           try {        
                 System.out.println("Waiting..."); // so we know we're waiting
                 receiveSocket.receive(receivePacket);
              } catch (IOException e) {
                 System.out.print("Stopped Listening for some reason..");
                 //e.printStackTrace();
            }

            System.out.println("Server received something" );

           //Constructor of ServerConnectionManager
             ServerConnectionManager serverConnectionManager = new ServerConnectionManager(receivePacket);
             Thread managerThread = new Thread(serverConnectionManager, "connectionManager ");
             managerThread.start();

                 //some more stuff to be done

                }

        }

The problem is that I can not call any methods on receivedPacket from within ServerConnectionManager run method. However, I am able to call receivedPacket.getPort() from within the constructor of this ServerConnectionManager thread and it gives me an expected output. But it does not do anything from within run method. The last line ServerConnectionManager prints is "Point A". Nothing after that!! Please check my DEBUGGING comments around that area to get a better idea of what I am talking about.

I know I have provided alot of code. But I can not understand the problem at all. I have tried passing additional parameters(objects) from Thread B to the constructor of ServerConnectionManager. And I am able to access those from the run method of ServerConnectionManager. Its just the receivedPacket that does not work...

回答1:

You need to create a new DatagramPacket per receive if you want to start a new thread to handle it. Otherwise one thread is synchronized on it during receive() while the other thread is trying to call getPort(). The design is invalid in any case, as the receive() will overwrite everything in the previously received datagram while the thread is trying to process it.