user3284469
user3284469

Reputation:

Will a Java thread be adopted by another thread if the thread which created it exits while it is running?

In Linux, if a process forks a child process, and then exits while the child process continue running, the child process will be adopted by init process.

I wonder if something similar also happen to java threads?

Is there parent-child relationship between Java threads?

When a Java thread creates another thread and doesn't call join() to wait for the other thread to finish running, and exists while the other thread is running, will the other thread be adopted by some other Java thread?

Thanks.

Upvotes: 1

Views: 150

Answers (2)

Is there parent-child relationship between Java threads?

No, there is no such relationship.

While most of the discussion in the preceding chapters is concerned only with the behavior of code as executed a single statement or expression at a time, that is, by a single thread, the Java Virtual Machine can support many threads of execution at once. These threads independently execute code that operates on values and objects residing in a shared main memory. Threads may be supported by having many hardware processors, by time-slicing a single hardware processor, or by time-slicing many hardware processors.

Threads are represented by the Thread class. The only way for a user to create a thread is to create an object of this class; each thread is associated with such an object. A thread will start when the start() method is invoked on the corresponding Thread object.

Java SE > Java SE Specifications > Java Language Specification > Chapter 17. Threads and Locks.

Also, please refer to the documentation of the Thread class (Java Platform SE 8): there is no mention of such relationship here.


When a Java thread creates another thread and doesn't call join() to wait for the other thread to finish running, and exists while the other thread is running, will the other thread be adopted by some other Java thread?

No, the created (second) thread will not adopted by any other thread. But, please note:

The Java Virtual Machine continues to execute threads until either of the following occurs:

  • The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.
  • All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.

Thread class (Java Platform SE 8).

Upvotes: 1

user207421
user207421

Reputation: 310883

Is there parent-child relationship between Java threads?

No. And therefore no adoption either.

Upvotes: 1

Related Questions