Can not access object's properties(methods) from within the run method! Java Multithreading

前端 未结 1 1522
再見小時候
再見小時候 2021-01-28 13:35

I have the following code in ServerConnectionManager:

public class ServerConnectionManager implements Runnable {

    private DatagramPacket receivedPacket;
             


        
相关标签:
1条回答
  • 2021-01-28 13:54

    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.

    0 讨论(0)
提交回复
热议问题