How to create a daemon thread in java

Welcome to Toppersarena.com Forums Computer Language Arena How to create a daemon thread in java

This topic contains 5 replies, has 0 voices, and was last updated by Admin admin 5 years, 4 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1851
    Admin
    admin
    Keymaster

    How to create a daemon thread in java

    A daemon thread is simply a background thread that is subordinate to the thread that creates it, so when the thread that created the daemon thread ends, the daemon thread dies with it.

    A thread that is not a daemon thread is called a user thread. We cannot create a thread directly as daemon, first we need to create a user thread then we can convert user defined thread as daemon thread.

    To create a daemon thread, You can call setDaemon() method on user defined thread with a true boolean value as argument before the thread is started.

    Below in the code example of creating daemon thread

    package com.java.connect.thread;

    //How to create a daemon thread in java
    public class DaemonThreadExample implements Runnable {

    private String name;

    public DaemonThreadExample(String name) {
    this.name = name;
    }

    public void run() {
    System.out.println(“Thread ” + name + ” running”);
    }

    public static void main(String[] args) {
    Thread thread1 = new Thread(new DaemonThreadExample(“First”));
    Thread thread2 = new Thread(new DaemonThreadExample(“Second”));

    // Set thread1 is as daemon thread
    thread1.setDaemon(true);
    thread1.start();

    // thread2 is a user thread
    thread2.start();
    // Print the result to find the daemon thread or not.
    System.out.println(“Thread1 is daemon thread : ” + thread1.isDaemon());
    System.out.println(“Thread2 is daemon thread : ” + thread2.isDaemon());
    }
    }

    #3227
    Admin
    admin
    Keymaster

    New to java and found it really interesting to see a use of that daemon thread.
    Java is really a vast language there is always new after some time to learn.

    #3228
    Admin
    admin
    Keymaster

    But What is the difference between thread and daemon thread..?

    #3229
    Admin
    admin
    Keymaster

    Hi,

    The difference between these two types of threads is straightforward: If the Java runtime determines that the only threads running in an application are daemon threads (i.e., there are no user threads in existence) the Java runtime promptly closes down the application, effectively stopping all daemon threads dead in their tracks. In order for an application to continue running, it must always have at least one live user thread. In all other respects the Java runtime treats daemon threads and user threads in exactly the same manner.

    Thanks
    Aniruddh

    #3232
    Admin
    admin
    Keymaster

    Thanx..4 ur rply…
    Bt m still nt getng the correct diffrnce.plz explain me more clearly

    #3230
    Admin
    admin
    Keymaster

    Hello,

    In java we have two type of Threads :
    Daemon Thread and User Threads. Generally all threads created by programmer are user thread (unless you specify it to be daemon or your parent thread is a daemon thread). User thread are generally meant to run our programm code. JVM doesn’t terminates unless all the user thread terminate.

    On the other hand we have Daemon threads. These threads are generally ‘Service provider’ threads. They should not be used to execute your program code but system code. These thread run in parallel to your code but JVM can kill them anytime. When JVM finds no user threads it stops and all daemon thread terminate instantly. Thus you should never depend on daemon thread to perform any program code.

    Hope that answers your query.
    Thanks
    Dhruv

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.