473,473 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Daemon Thread and Non-Daemon Thread.

dmjpro
2,476 Top Contributor
Expand|Select|Wrap|Line Numbers
  1. class MainClass{
  2.  public static void main(String a[]){
  3.   //do something
  4.  }
  5. }
  6.  
When i run this class file then JVM is the process having one main thread.
Thread ends and process ends.
I went through a link there i read about daemon thread.
"a daemon thread is one which won't prevent the program from ending." Basically it means main program/main thread.

Expand|Select|Wrap|Line Numbers
  1. class HelloThread extends Thread  
  2.  {  
  3.    public void run()  
  4.    {  try{
  5.      for ( ; ; )  
  6.      {  
  7.        System.out.println("hello");  
  8.        sleep(1000);  
  9.      }  }catch(Exception e){}
  10.    }  
  11.  }  
  12.  public class ThreadTest  
  13.  {  
  14.    public static void main(String[] args)  throws Exception
  15.    {  
  16.      Thread hello = new HelloThread();
  17.      hello.start();
  18.      System.out.println("Sorry, I must be leaving");  
  19.    }  
  20.  }
  21.  
That means here HelloThread is non-daemon. Here main program finishes but HelloThread still running. So all the threads are by default non-daemon?
May 7 '09 #1
13 9245
JosAH
11,448 Recognized Expert MVP
Near the start of the Thread API documentation:

Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.

When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class).
kind regards,

Jos
May 7 '09 #2
dmjpro
2,476 Top Contributor
Well ....
I have some definitions of threads. Daemon means Devil(violates the normal actions). Is it?
Daemon Thread: It won't prevent program from ending. That means if the wants to stop and then Daemon Thread won't stop it from ending.
Anything wrong in my conception here?
Here the term program, what does it mean? Please clarify me!

In my case the New Thread is Daemon or Non-Daemon and Why?
May 8 '09 #3
r035198x
13,262 MVP
Which parts of the quoted text above do you not understand?

You don't know what a program is??
May 8 '09 #4
dmjpro
2,476 Top Contributor
The second code...Main thread creating a New Thread.
Here program means .. the creating thread. In my previous example Main Thread?
May 8 '09 #5
r035198x
13,262 MVP
See the quoted text above. Do you see the word "program" there?
May 8 '09 #6
dmjpro
2,476 Top Contributor
Yeah seen.... But here program means the Main Thread which calls the main method?
May 8 '09 #7
JosAH
11,448 Recognized Expert MVP
@dmjpro
I had hoped that the little quote had tickled your curiosity; if you had read the API documentation, you'd have seen the next sentence:

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.
The main thread is not a daemon thread and when it dies and no other non-daemon threads are alive the entire jvm will shut down.

kind regards,

Jos
May 8 '09 #8
r035198x
13,262 MVP
You are no longer making much sense. Just read the quoted text above and stick with terminology used in that text.
May 8 '09 #9
dmjpro
2,476 Top Contributor
But the thing is in my example the New Thread is Daemon or Non-Daemon?
As Josh said if no Non-Daemon threads alive then JVM shuts down. If the creating thread is Daemon then new threads will be Daemon. Is it applicable for Non-Daemon? i think .. no ;)
Another tutorial says .... Daemon thread is the service providers for other threads.
Here other threads mean both or Non-Daemon only? So "if no Non-Daemon threads alive then JVM shuts down" this line valid. If there no Non-Daemon threads then to whom service to be provided.
Up to this is cleared so far (having some confusion what i asked).
Now come to my program... The main thread creates the new thread which is simply running an infinite loop. Now here main thread ends but the new thread still running. If the set the Daemon flag true using setDaemon(true) then new thread end up before the main thread. So the new thread is a non-daemon as it changes the behaviour after changing the daemon flag to true. In which sense the new thread is called Non-Daemon?
May 8 '09 #10
JosAH
11,448 Recognized Expert MVP
There is nothing special about deamon threads or non-daemon threads. A (non)daemon thread creates (non)daemon threads. The status of a thread can be changed with the setDaemon(...) call before a thread is started.

If no non-daemon threads are alive anymore the JVM will quit. That is all there is to it.

kind regards,

Jos
May 8 '09 #11
JosAH
11,448 Recognized Expert MVP
@r035198x
I know; that's what my mother already said ages ago ;-)

kind regards,

Jos
May 9 '09 #12
r035198x
13,262 MVP
You snuck your response between mine and dmjpro's just to make me look silly.
May 11 '09 #13
JosAH
11,448 Recognized Expert MVP
@r035198x
I didn't do it! I'm innocent! One of my parrots walked over my keyboard; naughty parrot! No cookie! Naughty!

kind regards,

Jos :-D
May 11 '09 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
8
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
0
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.