0
public void run() {
        assignPlayer();

        for(int i = 0; i < numPlayers; i++) {
            PlayerListener listener = new PlayerListener(fromPlayer.get(i), this, i);
            new Thread(listener).start();
        }

        return;
}

I am implementing a socket game. Every game has 2 players, and every game is given its own thread. the above run method assigns PlayerListener(which is a Runnable object) to each player for listening their incoming outputstream and calls some method from the game object if there is action to perform. Everything runs fine, but I was thinking about when will my game thread dies, so I intentionally set "return" after the PlayerListener assignment is finished ( but I think after the assignment is finished, there will be no statement so the run method will run anyway ). Anyway, my game is still running, shouldn't my thread dies and my game object dies along with it?

It is because the PlayerListener objects still calling the game object's method from time to time so the object does not get garbage collected??

I read the post When does a Java Thread reach the 'Die' State. It states that "If the run() method returns", so I was curious...

Community
  • 1
  • 1
Joshua
  • 305
  • 4
  • 16
  • 3
    But you started `numPlayer` threads...Assuming that your loop within `run` is actually a `Thread`, once `run` exists the thread will die. What evidence do you have that it doesn't? The JVM won't stop until the only threads remaining are daemon threads... – MadProgrammer Apr 04 '14 at 01:06
  • Yep, and that's the key -- you're running non-daemon threads that are still alive. – Hovercraft Full Of Eels Apr 04 '14 at 01:08
  • Because the game is from the server, the players listen to the server(the game), if the dies, the game will not provide info to the players, but in my case the players still get response. so I guess it did not die? – Joshua Apr 04 '14 at 01:10
  • @MadProgrammer are you saying since PlayListener(a thread) objects still running, so the run method from the game will not die as long as the PlayListener objects exist? – Joshua Apr 04 '14 at 01:12
  • 2
    Without knowing what the main `run` method is (ie implemented from a Runnable or Thread or just a plain method), it's impossible to be 100% sure, but while there are non-daemon threads running, the JVM will not exit. From your example, the `run` method will exit normally, once the loop is completed – MadProgrammer Apr 04 '14 at 01:19
  • the main run method(above) is from the game object that implements Runnable. When you say exit, you mean the game thread would die, but the game object kept alive? – Joshua Apr 04 '14 at 01:24

2 Answers2

2

so I intentionally set "return" after the PlayerListener assignment is finished

Your return; is extraneous since it's at the end of the method. The method would return without it. The only time a thread will wait for the threads it forks is if you specifically call thread.join() on each of the threads.

Anyway, my game is still running, shouldn't my thread dies and my game object dies along with it?

The threads that you fork are most likely non-daemon threads. Daemon threads exit when the JVM exits but the JVM waits for all non-daemon threads to exit on their own. When you fork threads they take on the daemon status of the thread that forked them. If you want these threads to quit once the run() method finishes then say something like:

Thread thread = new Thread(listener);
// make sure the threads will be killed when all other threads finish
thread.setDaemon(true);
thread.start();

I read the post When does a Java Thread reach the 'Die' State. It states that "If the run() method returns", so I was curious...

Right. If you are in a thread's run() method, then that thread will finish if the run() method finishes. However, again, the threads that are forked in the loop will keep the application running unless they are specifically set as being daemon threads.

Gray
  • 115,027
  • 24
  • 293
  • 354
1
  1. The "return;" statement does nothing, since it's at the end of a void method (and the compiler automatically adds code to a void method as if there were a "return;" statement there).

  2. You haven't really asked a clear question. Are you asking why the program doesn't terminate?

  3. You didn't explain how this particular "run()" method gets called. Only if it is the run() method of the thread itself, and called as part of the thread starting, will its return cause the thread to die.

  4. Furthermore, the process will not die as long as there is at least one non-daemon thread running.

All in all, you have provided a patchwork of partial information, and a patchwork of partial questions. Connecting those two patchworks is beyond the abilities of Agatha Christie, let alone anyone with perfect knowledge of Java, let alone people on this site.

cpurdy
  • 1,177
  • 5
  • 12