I’m currently developing the networking for my game, and have a client and a server running. The server currently loops infinitely in another Thread, but my client and game code are in the same thread. I have ran into problems when the client is handling data from the server, and the game hangs until the client is done processing new packets.
I tried to solve this, by making the client class implement Runnable, and run in a separate thread. I have run into some other errors, and am wondering if this is the problem.
I have the run method, and the sendPacket method:
public void run() {
// empty
}
public void sendPacket() {
somePacketSendingCode();
}
There is no code in the run method, as I only use the sendPacket method. sendPacket is called by a listener thread when a packet is received.
If I have no code in the run method, does that mean that the client Thread stops executing after starting? If this is so, doesn’t that mean that the sendPacket method would do nothing?
If you are not calling the
sendPacketmethod inside therunmethod, then it will never execute. The thread stops as soon asrunreturns.Note that only the run method contains the actual code of the thread. You said in your post that you have the
sendPacketmethod and are only using that one. This means that you are not actually running anything in parallel. A parallel thread will be fired when you callstart(), which calls therunmethod asynchronously. Calling onlysendPacketis not parallelism.