473,373 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,373 software developers and data experts.

Daemon Thread and Non-Daemon Thread.

2,476 2GB
Expand|Select|Wrap|Line Numbers
  1. class MainClass{
  2.  public static void main(String a[]){
  3.   //do something
  4.  }
  5. }
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.  }
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 9231
11,448 Expert 8TB
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,

May 7 '09 #2
2,476 2GB
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
13,262 8TB
Which parts of the quoted text above do you not understand?

You don't know what a program is??
May 8 '09 #4
2,476 2GB
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
13,262 8TB
See the quoted text above. Do you see the word "program" there?
May 8 '09 #6
2,476 2GB
Yeah seen.... But here program means the Main Thread which calls the main method?
May 8 '09 #7
11,448 Expert 8TB
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,

May 8 '09 #8
13,262 8TB
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
2,476 2GB
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
11,448 Expert 8TB
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,

May 8 '09 #11
11,448 Expert 8TB
I know; that's what my mother already said ages ago ;-)

kind regards,

May 9 '09 #12
13,262 8TB
You snuck your response between mine and dmjpro's just to make me look silly.
May 11 '09 #13
11,448 Expert 8TB
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

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...
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...
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
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...
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...
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...
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...
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...
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.