What is a daemon thread?

hi all,
can somebody plz tell me what is daemon thread and what is the difference between daemon thread a user thread. thanx in advance..

Daemons are background programs that run all the time
during up-time, ready to do something that may be
required at any point. These are NOT limited to the
JVM, but the concept is mirrored in teh ability to
set a java thread as daemon, as the JVM is just that:
a Virtual Machine that mimics an environment.
Correspondingly OSs have daemon processes (called
d services on windows) e.g. for printing. Calling
Thread.setDaemon() will I believe mark the thread as
to not be destroyed until VM shutdown, and will not
itself stop shutdown of the VM if it is still running.Maybe I got confused by all those nots but if the only threads running are daemon
threads, the VM is allowed to stop; I made that mistake myself several times.
This is what I ripped from the API docs:
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.
It's the "continues to execute until' ... 'all threads that are *not* daemon threads
have died" part. They should have worded that a bit less cryptic ;-)
kind regards,
Jos
ps. First I thought daemon threads run under your bed while drooling sometimes ;-)

Similar Messages

  • What is a daemon thread in java ?

    What is a daemon thread in java ? Explain with a small example.

    Any thread marked as a daemon. There is nothing special about it. The only intersting thing is that the Java VM terminates if no non-daemon thread is running. So you usually use a daemon thread for bookkeeping tasks, so that this thread doesn't prevent the VM from successfully terminating.

  • Thread pool of daemon threads?

    If I create a Thread, it can be made a daemon thread by invoking setDaemon(true). But what if I create a thread pool via java.util.concurrent.Executors.newFixedThreadPool(NTHREADS)? How can those threads be made daemon threads?
    CBy

    Thanks Joachim! I didn't know that the newFixedThreadPool method was overloaded.

  • Java FX task daemon thread

    Hello,
    I'm working on a large scale GUI and running in to OOMs. Looking at the threads, there are hundreds of them sitting around named Java FX task daemon thread.
    Daemon Thread [Java FX task daemon thread] (Running)            
                                    Daemon Thread [Java FX task daemon thread] (Suspended)      
                                                    Unsafe.park(boolean, long) line: not available [native method]              
                                                    LockSupport.park(Object) line: 158        
                                                    AbstractQueuedSynchronizer$ConditionObject.await() line: 1987          
                                                    DelayQueue<E>.take() line: 160              
                                                    ScheduledThreadPoolExecutor$DelayedWorkQueue.take() line: 609   
                                                    ScheduledThreadPoolExecutor$DelayedWorkQueue.take() line: 602   
                                                    ScheduledThreadPoolExecutor(ThreadPoolExecutor).getTask() line: 947            
                                                    ThreadPoolExecutor$Worker.run() line: 907      
                                                    Thread.run() line: 662    Can anyone shed some light on what is starting these threads?

    I had similar issues with a server running on XP, where it kept dying randomly with no problems.
    I tracked down what was happening by getting the exit value of the java process - which may give you information regarding what happened to the process.
    For example, if CTRL+C was pressed you get a value of 130 returned in the Linux environment. Using this error information I found out why my server kept dying (which was the XP telnet window was sending dodgy key presses to the window the server was running in).
    The exit code of a process is available after it exits via $status in Unix or %errorlevel% in a PC .bat file.

  • Daemon Threads

    Hi, what I want is to connect from a daemon thread to Ldap, if I set the thread to daemon=false it works, if I set it to daemon=treu; it does'nt work anymore.
    Thanks.

    The problem was in fact when I was trying to wrote to file, and not the connection to Ldap. When the thread was set to daemon=false it worked, and I could see that the connection to Ldap was working, but when it was set to daemon=true, the thread couldn't write t the file and I thought that the connection was the problem. Anyway, thanks, the problem is solved now.

  • Daemon Threads: Can you explain this behaviour

    Hey All,
    Please refer to code segment given below. As per the defination of Daemon threads it should terminate when all other user threads die. In this case it should terminate when main thread ends as thread2 has not been initiated yet.
    The result is not consistent. We get the expected output sometimes, whereas sometimes program goes in to an infinite loop and other times excutes a few lines of thread2 and then quits.
    Can someone explain this?
    Thank you
    Vidur Gupta
    class simpleThread extends Thread
         public void run()
              System.out.println("Thread 1 daemon starts");
              babyThread thread2 = new babyThread(); //Thread2 created
              thread2.setDaemon(false); //Thread2 daemon=false
              thread2.start();          
              System.out.println("Thread 1 daemon status= "+ this.isDaemon());
              System.out.println("Thread 1 ends"); //thread1 ends
    class babyThread extends Thread
         public void run()
              System.out.println("Thread 2 starts");
              while(true) //Thread2 goes in to an infinite loop
              System.out.println("Thread 2 daemon status= "+ this.isDaemon());
    public class daemon
         public static void main(String[] args) //Main
              int i=0;
              simpleThread thread1= new simpleThread(); //Create thread1
              thread1.setDaemon(true); //Set Thread1 as Daemon
              thread1.start();
              while(i<1000000)
                   i++;
              System.out.println("Thread Main ends"); //Main ends
    }

    It's not clear what behavior you expect, or what behavior you're observing, but here's what I would expect to happen:
    thread1.start() is called.
    After that, either of two things could happen:
    1) the main thread could go into your very short spinlock counting up to 1000000
    OR
    2) thread1 starts running
    You can't predict or control which one of those happens, and it may be that on successive runs of your program, one happens sometimes and the other one happens other times.
    At some point, you'll get to where the main thread dies. If the only other thread that has been started is thread1, then, since it's a deamon, the program will die.
    However, it may be that sometimes that happens, and other times, thread1 gets enough CPU time to call thread2.start(). You may not see any output from thread2 or see the ""Thread 1 daemon status" output that comes right after t2.start() before main ends, but if t2 has been started, then you've got another non-deamon.
    Bottom line: Whether or not there's an additional non-deamon besides main depends on how the scheduler happens to schedule your threads, and you should not expect that to be consistent or predictable.

  • Garbage collector as daemon thread

    hai
    y is garbage collector made as daemon thread
    what r the primary reasons?

    http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=1&t=012268
    Are you guys in the same class?
    The Truth About Garbage Collection
    Reference Objects and Garbage Collection
    Garbage collection and performance
    Also, please make the extra effort to write out words such as "why" and "are". The extra keystrokes won't cost much in the way of time, and the enhanced clarity will be appreciated by those communicating on a forum with international readership.
    http://faq.javaranch.com/view?UseRealWords

  • Need help  regarding Daemon threads

    Hi to All
    What are deamon threrads ?
    Why we need to set Deamon(true)?
    explain me clearly
    I know deamon threads are low prior
    Again why we need to set to true.
    Help me

    You have two threads running. Neither one is daemon. One ends. The other keeps on running.
    You have two threads running. One is daemon. The daemon thread ends. The other keeps on running.
    You have two threads running. One is daemon. The other thread ends. The application ends because the only remaining thread is a daemon.
    You have five threads running. Three are daemon. A non-daemon thread ends. All other threads keep on running. A daemon thread ends. All other threads keep on running. The only remaining non-daemon thread ends. The application ends because the only remaining threads are daemons.
    Got it?

  • Thread Operation - daemon thread

    What will be the output of the following program (with explaination)?
    public class A extends Thread {
    A() {
    setDaemon(true);
    public void run() {
    (new B()).start();
    try{
    Thread.sleep(60000);
    } catch (InterruptedException x) {}
    System.out.println("A done");
    class B extends Thread{
    public void run() {
    try{
    Thread.sleep(60000);
    } catch (InterruptedException x) {}
    System.out.println("B done");
    public static void main(String[] args) {
    (new A()).start();
    }

    If you consult the documentation for Thread.setDaemon to see what a daemon thread means; and the docs for the constructors of Thread to see what the default daemon status is, then you will understand why this program prints nothing.
    Now if you moved those prints ahead of the sleeps it might print something but it need not - all depends on scheduling.

  • What happened to this thread? -- LabVIEW poll "Features you would like to see in LabVIEW!"

    What happened to this thread? --> LabVIEW poll "Features you would like to see in LabVIEW!"

    Warren Massey -
    Unfortunately, we were not able to migrate the polls and topics to the new system when we launched. The good news is that we still have access to the data and are investigating our migration options available.
    We will also focus on getting some new topics and polls out to the community in the next few weeks.
    Thanks!
    Molly K.
    Web Support & Operations Manager
    National Instruments

  • How to use a thread as daemon thread

    In multithreading i have undergone through daemon thread concepts and its mentioned as low priority thread which runs in the background of garbage collection.
    My query is how to make the thread daemon thread r we going to use thread priorities here.
    Can anyone demonstrate with an example reg this. I will be very much thankful to them.

    In general, threads that run application code are not daemon threads, and threads that run system code are daemon threads,so the daemon attribute is queried using the isDaemon() method; it is set using the setDaemon() method.....

  • What happen to julius thread in ABAP DEVELOPMENT?

    It was reformatted by the new 5000 characters per post limit
    I am busy working on a solution to split them and move each part to its own respecting sub-development forum - but have not got to it yet. Sorry for being slow.
    The FAQ and memorable discussions thread in the Security Forum is affected as well, but there is only one forum... :-((
    Any creating ideas on how to fix that one (other than removing information from it)?
    Cheers,
    Julius

    Actually it is happening since couple of week. Might be you have found yourself in this situation now.
    Also there are two thread already open on the same. Perhaps you missed them.
    Improper display of threads
    And outstandingly "what happen to julius thread in ABAP DEVELOPMENT?" Nice Subject Line
    Perhaps, It looks [Line of the day|Re: Line of the day!] !!
    P.S. Subject tempered (Or Perhaps corrected)....;-)
    Anyways,
    @ Julius: Since there is 5000 Characters Limit has been introduced for keeping the forum neat, undersized and informative as much as possible rather than long bulky programs.
    But My Question is What about the Programs and Threads which had already been posted in last 5-6 Years of SDN(now SCN)?
    I can see all the previous threads also have been affected!
    So Does SAP having any Programmer In their team of Michael Goeck or David Branan(Who is working from behind the scene ) to introduced the restriction of 5K chars based on Date?!?
    Cheers

  • Daemon thread still alive when the environment is closed

    I'm using the replication framework with the Java API in an OSGi container (equinox). I notice that when I'm closing the environment there is a deamon thread still alive. Each time I start a new replication environment and shut it down, one more daemon thread is added (and still running).
    Is there a way to close the replication framework and all their associated threads?
    I'm using BDB 4.7.25 - windows
    thanks
    dong

    Hi Dong,
    I don't know of a way to force the removal of the daemon thread(s).
    I believe that the thread is actually gone - as long as you are closing down the environment cleanly. The alternative to using daemon threads is to ensure that DetachCurrentThread is called by the JNI layer for each thread. Given the way the Berkeley DB Java API is implemented, that is not feasible.
    Are the additional threads causing issues?
    Regards,
    Alex

  • What is online-redo thread

    Hi all,
    what is online-redo thread
    from OTN docs..given below
    1.+Each database instance has its own online redo log groups. These online redo log+
    groups, multiplexed or not, are called an instance’s thread of online redo. In typical
    configurations, only one database instance accesses an Oracle database, so only one
    thread is present.
    2. When running Oracle Real Application Clusters, however, two or
    more instances concurrently access a single database and each instance has its own
    thread.
    my question is:
    1.where can we see the thread_no?
    2.when it can generate?
    Regards,
    DB.

    1.where can we see the thread_no?from v$log
    see below output from 2 node RAC system
    select * from v$log order by 2;
             GROUP#|         THREAD#|       SEQUENCE#|           BYTES|         MEMBERS| ARC| STATUS          |   FIRST_CHANGE#| FIRST_TIME
    ---------------| ---------------| ---------------| ---------------| ---------------| ---| ----------------| ---------------| -----------------
                  5|               1|          447903|       209715200|               2| YES| INACTIVE        |     86788699678| 27-JUN-2012 17:22
                  2|               1|          447902|       209715200|               2| YES| INACTIVE        |     86788542040| 27-JUN-2012 17:18
                  1|               1|          447904|       209715200|               2| NO | CURRENT         |     86788741960| 27-JUN-2012 18:07
                  4|               2|          164783|       209715200|               2| NO | CURRENT         |     86788742226| 27-JUN-2012 18:07
                  6|               2|          164781|       209715200|               2| YES| INACTIVE        |     86787946796| 27-JUN-2012 16:00
                  3|               2|          164782|       209715200|               2| YES| INACTIVE        |     86788375619| 27-JUN-2012 17:14
    6 rows selected.

  • Can anyone tell daemon thread in simple words with an example

    can anyone tell Daemon thread in simple words (i don't understand high level language)with an example

    He reminds me of a candidate I had the ... priviledge? ... of interviewing for a web developer position not too long ago. I asked him to "reverse the characters in a string, i.e. star = rats" using his language of choice. He stands, walks to the white-board and starts to write some stuff. At that time, I turned to my boss and we start making comments to each other about the guys resume and discussing some of our current workload, giving him time to work this code out on the board. A few minutes (5 - 10) go by and I glance up to see that he has written a single line of code (and that his hands are covered in dry erase marker from having scrubbed multiple attempts off the board). His code (I believe it as a stab at either php or javascript) read:
    "reverse_string('star');"
    I looked at my boss, he looked at me ... we both blinked a couple of times and then I explained, "no - I need you to implement the reverse_string method. I want you to write the code that does the actual reversing."
    "Oh. Yeah, I can't do that."

Maybe you are looking for