Daemon thread in java with example

Daemon threads are low priority background threads which provide services to user threads. It’s life depends on user threads. If no user thread is running then JVM can exit even if daemon threads are running. JVM do not wait for daemon threads to finish.
Daemon threads perform background tasks such as garbage collection, finalizer  etc.
The only purpose of daemon thread is to serve user thread so if there are no user threads, there is no point of JVM to run these threads, that’s why JVM exits once there are no user threads.

Two method related to daemon thread

Public void setDaemon(boolean status) :
This method can be used to mark thread as user  or daemon thread. If you put setDaemon(true), it makes thread as daemon.
 
Public boolean isDaemon()
This method can be used to check if thread is daemon or not.

Daemon Thread example:

When you run above program, you will get below output:

Thread 1 is user thread
Thread 3 is user thread
Thread 2 is daemon thread

Please note that you can not convert user thread to daemon thread once it is started otherwise it will throw IllegalThreadStateException.

When you run above program, you will get below output:

Thread 1 is user threadException in thread “main”
Thread 2 is user thread
Thread 3 is user thread
java.lang.IllegalThreadStateException
at java.lang.Thread.setDaemon(Thread.java:1388)
at org.arpit.java2blog.DaemonThreadMain.main(DaemonThreadMain.java:28)


import_contacts

You may also like:

Related Posts

  • Get Thread Id in Java
    30 September

    Get Thread Id in Java

    Table of ContentsGet Thread Id in JavaGet Thread Id of Current Running ThreadGet Thread id of Multiple Threads In this article, we will learn to get thread id of a running thread in Java. An Id is a unique positive number generated at the time of thread creation. This id remains unchanged during the lifetime […]

  • ArrayBlockingQueue in java
    19 September

    ArrayBlockingQueue in java

    Table of ContentsWhat is BlockingQueueArrayBlockingQueueFeaturesConstructorsMethodsUsage ScenariosImplementation CodeSummary In this article, we will understand the Java concurrent queue, BlockingQueue. We will then go deep into it’s one of the implementation, ArrayBlockingQueue. What is BlockingQueue BlockingQueue interface was introduced in Java 5 under concurrent APIs and it represents a thread-safe queue in which elements can be added […]

  • Delay java program by few seconds
    11 December

    Delay Java program for few secs

    Table of ContentsUsing Thread.sleepUsing TimeUnit.XXX.sleep methodUsing ScheduledExecutorService In this post, we will see how to delay java program for few secs. We need to delay a java programs in many situation where we want to wait for some other task to finish. There are multiple ways to delay execution of java program. Using Thread.sleep Sleep […]

  • 29 May

    How to print even and odd numbers using threads in java

    Table of ContentsProblemSolution 1Print even and odd numbers using threads in java Solution 2: Using remainder In this post, we will see how to print even and odd numbers using threads in java. see also: How to print sequence using 3 threads in java Problem You are given two threads. You need to print odd […]

  • wait(),notify() and notifyAll() in java
    22 October

    Why wait(), notify() And notifyAll() methods are in Object Class

    In this post, we will see why wait(), notify() And notifyAll() methods are in Object Class And Not in Thread Class. This is one of the most asked java multithreading interview questions. You might know that wait(), notify() And notifyAll() Methods are in Object class and do you know the reason for the same? Let’s […]

  • Custom BlockingQueue in java
    16 October

    Custom BlockingQueue implementation in java

    In this post, we will see how to create your own custom BlockingQueue. This is one of the most asked java interview questions. You need to implement your own BlockingQueue. This question helps interviewer to get your understanding of multithreading concepts. Here is simple implementation of BlockingQueue. We will use array to store elements in […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.