JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Java Daemon Thread Examples

  • You can make any java thread as daemon thread. Daemon threads acts like service providers for other threads running in the same process.
  • Daemon threads will be terminated by the JVM when there are none of the other threads running, it includs main thread of execution as well.
  • To specify that a thread is a daemon thread, call the setDaemon method with the argument true.
  • To determine if a thread is a daemon thread, use the accessor method isDaemon.

Daemon Thread Sample Code

package com.myjava.threads;

public class DaemonThread extends Thread{
    
    public DaemonThread(){
        setDaemon(true);
    }
    public void run(){
        System.out.println("Is this thread Daemon? - "+isDaemon());
    }
    public static void main(String a[]){
        DaemonThread dt = new DaemonThread();
        // even you can set daemon constrain here also
        // it is like dt.setDeamon(true)
        dt.start();
    }
}

Example Output

Is this thread Daemon? - true

Other Thread Examples

Knowledge Centre
What is servlet context?
The servlet context is an interface which helps to communicate with other servlets. It contains information about the Web application and container. It is kind of application environment. Using the context, a servlet can obtain URL references to resources, and store attributes that other servlets in the context can use.
Famous Quotations
Be yourself; everyone else is already taken.
-- Oscar Wilde

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.