I have a main thread which creates other thread(Name = TASK) which has task independent of main thread. If main thread finishes it works then is it possible that main exits and still other thread(TASK) can continue it's execution with out being a deamon thread. I am aware of the concept of deamon thread but we can't use it as when main thread exits,all other deamon threads die. If it's not possible then is there any workaround.
1 Answers
is it possible that main exits and still other thread(TASK) can continue it's execution with out being a deamon thread
That's the definition of a daemon thread. If you want the threads to continue executing then they should not be daemon. Daemon threads are killed by the JVM when the last user thread finishes. The JVM waits for non-daemon threads to finish before the JVM can terminate.
See:
- What is Daemon thread in Java?
- How do I create daemon threads?
- How to keep my program alive for as long a daemon thread is running?
FYI: When you fork a thread it takes the daemon status of the forking thread. The "main" thread is always non-daemon.
Thread thread = new Thread(new MyRunnable(...), "my runnable");
// make sure my thread is not a daemon thread so the JVM will wait for it
thread.setDaemon(false);
thread.start();
we can't use it as when main thread exits, all other deamon threads die.
Maybe you have the definition of daemon threads backwards? To quote from the Thread.setDaemon(...)
javadocs:
void java.lang.Thread.setDaemon(boolean on)
Marks this thread as either a daemon thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads.
-
"The Java Virtual Machine exits when the only threads running are all daemon threads." out of curiosity: what happens to the daemon threads when the jvm exits? – Marco Forberg May 29 '13 at 11:52
-
They are terminated/cancelled @MarcoForberg. – Gray May 29 '13 at 11:52
-
For better clarity check the following example There is server class that processes the client request,generate and save result in database.And I want to separate this work .After generating result i want to send the result to client first and concurrently data should be saved. – Mitesh May 29 '13 at 11:54
-
okay thx. hmmm should have known... java-code running without a jvm doesn't make sense i guess. – Marco Forberg May 29 '13 at 11:54
-
It's not about "cleanly" @MarcoForberg. The JVM will wait (yes forever if necessary) for a non-daemon thread to finish before it can terminate. – Gray May 29 '13 at 11:57
-
hmmm okay maybe that was not properly phrased. But i've seen many jvms hat had to be killed because of an unbroken `while(true)' loop – Marco Forberg May 29 '13 at 11:59
-
I recommend that your server thread be non-daemon @Mitesh. Then when it forks the client handlers, they will be non-daemon. However, if you want the JVM to terminate and kill the child threads, then the server could fork daemon children. It's your choice. – Gray May 29 '13 at 12:00
-
That's just bad programming @MarcoForberg. I write `while(true)` loops all of the time -- well `while (!Thread.currentThread().isInterrupted())` loops. :-) If written correctly they pose no perils. – Gray May 29 '13 at 12:01
-
"written correctly"... so true... ;) – Marco Forberg May 29 '13 at 12:02
-
I got the first part but couldn't understand the second part(with forking)@Gray – Mitesh May 29 '13 at 12:04
-
1WHen the server threads accepts a connection from the client, typically it starts a new thread to handle that connection. You have the choice to write the server so that it starts daemon threads or non-daemon threads. If you want the JVM to stay up only until the server thread finishes then make the client threads daemon. If you want the JVM to stay up until the server thread and all of the client threads finish then make the client threads non-daemon @Mitesh. – Gray May 29 '13 at 12:06