• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt in Thread and Daemon Thread  RSS feed

 
vishwa raj
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In case of non daemon threads when an exception is thrown that thread will die and callstackFrame will be disolved.But in case of Daemon Threads if the daemon thread throws some exception what will happen because Daemon is not in the control of JVM what i feel based on the code is that even in the case of Daemon Thread when there is exception thrown Daemon will die and callstackframe will be disolved


Thanks
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daemon thread will behave exactly the same as non-daemon thread, in this situation, and most others.

There is exactly one difference between daemon thread and non-daemon thread. That is in the JVM's decision of when to shut down. It shuts down when all non-daemon threads finish, and takes no account of daemon threads.

It is not true that daemon threads are outside the control of the JVM. They have all the same characteristics as non-daemon threads, with the above, single difference.
[ November 12, 2007: Message edited by: Peter Chase ]
 
vishwa raj
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the reply. The only differnce is that JVM will not wait for Daemon Thread to complete ok but say Daemon Thread is still under running and JVM decides to shut down now Daemon will be under the control of OS so Daemon is not under the control of JVM is my understanding correct.Please correct me if i am wrong.

Thanks
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, when the JVM shuts down, all daemon threads are killed.
 
vishwa raj
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i say Java filename JVM starts up and when the programme completes JVM shuts down. in case of Threads JVM will wait till all the non Daemon Threads
to complete and then shuts down with out waiting for Daemon Theads to complete there by killing Daemon Threads so if some job is getting processed by daemon it will be stopped in between. Then what is the use of having Daemon

According to definition of Daemon Thread it should be always running but in this case it is dieing soon after shutdown of JVM. they are contradecting each other is there any links for resources so that i can get more knowledge on Threads and Daemons.

Thanks
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Goodness me, you're really having trouble with a terribly simple thing. You just won't let go of the idea that there's much more to it than there is. Read my first reply again. That's all there is. Nothing more. Nada. Rien.

As to what daemon threads are for...

Daemon threads are typically used for long-running background tasks. However, there is nothing about the definition of a daemon thread that means it has to run forever, or even for a long time.

Daemon threads typically work with data that's all in-heap, as the heap is disposed on shut-down anyway. Examples might be indexing or performing some low-priority clean-up, on an in-memory data structure.

Deamon threads should not be used for any task that has periods when it would be unsafe to kill the task. For instance, a daemon thread typically should not be used for updating a database, as it might get killed half way through an update, leaving the database corrupt.

Not every application suits daemon threads. If yours doesn't, that's fine.
[ November 13, 2007: Message edited by: Peter Chase ]
 
vishwa raj
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the information I understood.

Thanks
 
Henry Wong
author
Sheriff
Posts: 22853
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to look at it.... Daemon processes running in the OS and Daemon threads running in a JVM, are two different things.

Henry
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed.

The Java language designers presumably used the term because they expected daemon threads to be used for the sort of long-running background task that characterises Unix daemon processes.

But, while Java daemon threads can be used for that sort of thing, they are not explicitly restricted to it. As mentioned in previous posts, they are in fact exactly the same as ordinary threads, with exactly one difference - they are ignored in the test for when to terminate the JVM.
 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent explanation Peter !!

And Henry, it's good that you pointed that difference out ! It's so very relevant to the discussion.
[ November 15, 2007: Message edited by: san ban ]
 
Angel Jordan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Explanation!!

[DELETED -- Please start new questions in a new topic. You can link to this topic if you feel it is somewhat related. But please don't hijack -- HENRY]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!