Reputation: 14490
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?
Upvotes: 15
Views: 22997
Reputation: 533492
After a run() method returns, the thread terminates. If you have called sendPacket, any packet sent will have been passed to the OS and it will pass on the packets. Any packet not sent will have to be sent by another thread or be lost.
Upvotes: 4
Reputation: 62439
If you are not calling the sendPacket
method inside the run
method, then it will never execute. The thread stops as soon as run
returns.
Note that only the run method contains the actual code of the thread. You said in your post that you have the sendPacket
method 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 call start()
, which calls the run
method asynchronously. Calling only sendPacket
is not parallelism.
Upvotes: 7
Reputation: 3182
AFAIK, the thread is stopped when the run () method returns. Thread States
Upvotes: 6