问题描述
我在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输出的最后一行是Point 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.
这篇关于无法从run方法中访问对象的属性(方法)! Java多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!