3

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

8
  • Apart from "this is not the proper way to do it": Did you try adding a thread.join() at the end of main? Commented Jun 27, 2018 at 6:22
  • Usually a thread is considered 'terminated' when the exit() method is run. Have you debugged the state of the thread after that point? Commented Jun 27, 2018 at 6:26
  • @daniu I tried it with join() as well as with yield(), did not change anything. But what would be the proper way to do it then? Commented Jun 27, 2018 at 6:53
  • 1
    @DavidStuder In your example, no thread (neither the main thread nor the other started thread) is running. This program directly terminates. Maybe you simplfied your code too much. Look at my examples to get an idea. Otherwise please make a minimal reproducible example. Commented Jun 27, 2018 at 7:00
  • 1
    Ok. This is weird. Obviously both threads are terminated, but the application is still running. That might be an issue of Eclipse but it might also be an issue of the Java VM. Maybe a reinstall will help here. This is not normal behavior and has nothing to do with your Java code. Commented Jun 27, 2018 at 7:13

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

9 Comments

But why is the other thread still running if it has been exited?
@daniu It is running because it is not terminated (or exited as you say).
OP does state he found out that "the last called method is the exit()-method of the Thread-class" when debugging though.
@daniu Well ... This is because one thread is terminated: the main thread! I still speak about my example. I think, OPs problem is vice versa. The main thread is still running but the other thread terminated. That means that the main thread is still doing something.
@DavidStuder Hmmm. I would say that in your example both threads are properly terminated. In that case the application (the Java VM) should also terminate.
|
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.