David Studer
David Studer

Reputation: 135

Java Main Class Thread does not terminate anymore using Java multithreading

I created my own thread class implementing the Runnable interface. But every time I start running my own thread class as a new thread, the main class thread does not terminate anymore by itself. Is this just an issue within Eclipse or would I also have problem running this on a Server? Do I have to change something calling the thread so that the main method can terminate properly?


Here's my basic self-made thread:

public class OwnThread implements Runnable {
   @Override
   public void run() {
      //do something
   }
} 

Here's the main class that won't terminate anymore:

public static void main(String[] args) {    
   Thread thread = new Thread(new OwnThread());
   thread.start();
}

When I debug it, the last called method is the exit()-method of the Thread-class. After going through these lines of code, the process goes on forever:

/**
 * This method is called by the system to give a Thread
 * a chance to clean up before it actually exits.
 */
private void exit() {
   if (group != null) {
      group.threadTerminated(this);
      group = null;
   }
   /* Aggressively null out all reference fields: see bug 4006245 */
   target = null;
   /* Speed the release of some of these resources */
   threadLocals = null;
   inheritableThreadLocals = null;
   inheritedAccessControlContext = null;
   blocker = null;
   uncaughtExceptionHandler = null;
}

Here's a screenshot of the thread that is running forever. The TestInterface class is where the main-method is located:

Thread running on forever

Upvotes: 3

Views: 2085

Answers (2)

David Studer
David Studer

Reputation: 135

The issue is indeed not caused by the multithreading logic itself, it is caused by Eclipse and the respective JVM. Running the exact same code in Netbeans or on an Tomcat 8 Server did not lead to any problems. A reinstallation of Eclipse did not solve the malfunction within the Eclipse framework, but having the certainty that the issue does not cause any trouble on a server is sufficient for me to close the case.

Thanks to Seelenvirtuose for the hints and his effort.

Upvotes: 2

Seelenvirtuose
Seelenvirtuose

Reputation: 20618

But every time I start running my own thread class as a new thread, the main class thread does not terminate anymore by itself.

This is somewhat wrong. Your program does not terminate because there exists at least one non-daemon thread that still is running. The rule is: A Java program is terminated if all non-daemon threads are terminated.

I modified your program to make this behavior clear:

public class OwnThread implements Runnable {
    @Override
    public void run() {
        runForever();
    }

    public static void main(String[] args) {
        Thread thread = new Thread(new OwnThread());
        thread.start();
        runForever();
    }

    private static void runForever() {
        while (true) {}
    }
}

Running that will create two threads that will run forever. One is the main thread which is started by running the program, and the other is the thread started inside the main method:

enter image description here

Modifying the above code by removing the call to runForever in the main method ...

public static void main(String[] args) {
    Thread thread = new Thread(new OwnThread());
    thread.start();
}

... will result in a different thread picture:

enter image description here

Here the main thread is gone because it is terminated. But the other started thread is still running.

Side note: Suddenly another thread appears - DestroyJavaVM. Have a look at the post DestroyJavaVM thread ALWAYS running for more information.

Upvotes: 4

Related Questions