无法从 run 方法中访问对象的属性(方法)!Java 多线程方法、多线程、属性、对象

2023-09-07 13:06:55 作者:失魂°

我在 ServerConnectionManager 中有以下代码:

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
                    }


         }

我在其他一些使用 ServerConnectionManager 的线程的 run 方法中有一些代码:让我们调用这个线程 B

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

                }

        }

问题是我无法从 ServerConnectionManager 运行方法中调用 receivedPacket 上的任何方法.但是,我可以从这个 ServerConnectionManager 线程的构造函数中调用 receivedPacket.getPort(),它给了我一个预期的输出.但它不会在 run 方法中做任何事情.ServerConnectionManager 打印的最后一行是A 点".之后什么都没有!!请查看我在该区域的调试评论,以更好地了解我在说什么.

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.

我知道我提供了很多代码.但我根本无法理解这个问题.我尝试将附加参数(对象)从线程 B 传递给 ServerConnectionManager 的构造函数.我可以从 ServerConnectionManager 的 run 方法中访问它们.它只是没有工作的receivedPacket......

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...

推荐答案

如果你想启动一个新线程来处理它,你需要为每个接收创建一个新的 DatagramPacket.否则,一个线程在 receive() 期间对其进行同步,而另一个线程正在尝试调用 getPort().该设计在任何情况下都是无效的,因为 receive() 将在线程试图处理它时覆盖先前接收到的数据报中的所有内容.

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.