Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Java Daemon Thread

The document explains the concept of Daemon threads in Java, which are low priority threads that support user threads and terminate when all user threads are closed. It details characteristics of Daemon threads, methods to manage them, and provides examples demonstrating their usage and limitations. Additionally, it highlights the importance of setting a thread as a Daemon before it starts to avoid runtime exceptions.

Uploaded by

Ayush Jha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Java Daemon Thread

The document explains the concept of Daemon threads in Java, which are low priority threads that support user threads and terminate when all user threads are closed. It details characteristics of Daemon threads, methods to manage them, and provides examples demonstrating their usage and limitations. Additionally, it highlights the importance of setting a thread as a Daemon before it starts to avoid runtime exceptions.

Uploaded by

Ayush Jha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Page 1 of 6

WhiteboardWe tested
this page and blocked content comingJobs
from
Home AI Assistant Online Compilers Tools Art
potentially dangerous or risky sites. Allow this content View all block
only if you're sure it comes from safe sites.

SQL HTML CSS Javascript Python Java C C++ PHP Scala C#

Java - Daemon Thread

Daemon Thread in Java


A Daemon thread is created to support the user threads. It generallty works in
background and terminated once all the other threads are closed. Garbage collector is
one of the example of Daemon thread.

Characteristics of a Daemon Thread in Java

A Daemon thread is a low priority thread.

A Daemon thread is a service provider thread and should not be used as user
thread.

JVM automatically closes the daemon thread(s) if no active thread is present and
revives it if user threads are active again.

A daemon thread cannot prevent JVM to exit if all user threads are done.

Thread Class Methods for Java Daemon Thread


The following are the methods provided by the Thread class for Daemon Thread in Java -

Thread.setDaemon() Method: Marks this thread as either a daemon thread or


a user thread.

Thread.isDaemon() Method: Checks if this thread is a daemon thread.

Example of Java Daemon Thread


In this example, we've created a ThreadDemo class which extends Thread class. In main
method, we've created three threads. As we're not setting any thread as daemon thread,
Page 2 of 6

no thread is marked as daemon thread.


We tested this page and blocked content coming from
potentially dangerous or risky sites. Allow this content
only if you're sure it comes from safe sites. Open Compiler

package com.tutorialspoint;
class ThreadDemo extends Thread {
ThreadDemo( ) {
}
public void run() {
System.out.println("Thread " + Thread.currentThread().getName()
+ ", is Daemon: " + Thread.currentThread().isDaemon());
}
public void start () {
super.start();
}
}
public class TestThread {
public static void main(String args[]) {
ThreadDemo thread1 = new ThreadDemo();
ThreadDemo thread2 = new ThreadDemo();
ThreadDemo thread3 = new ThreadDemo();
thread1.start();
thread2.start();
thread3.start();
}
}

Output

Thread Thread-1, is Daemon: false


Thread Thread-0, is Daemon: false
Thread Thread-2, is Daemon: false

More Example of Java Daemon Thread

Example 1

In this example, we've created a ThreadDemo class which extends Thread class. In main
method, we've created three threads. As we're setting one thread as daemon thread,
one thread will be printed as daemon thread.
Page 3 of 6

We tested this page and blocked content coming from


Open Compiler
potentially dangerous or risky sites. Allow this content
only if you're sure it comes from safe sites.

package com.tutorialspoint;
class ThreadDemo extends Thread {
ThreadDemo( ) {
}

public void run() {


System.out.println("Thread " + Thread.currentThread().getName()
+ ", is Daemon: " + Thread.currentThread().isDaemon());
}
public void start () {
super.start();
}
}
public class TestThread {
public static void main(String args[]) {
ThreadDemo thread1 = new ThreadDemo();
ThreadDemo thread2 = new ThreadDemo();
ThreadDemo thread3 = new ThreadDemo();
thread3.setDaemon(true);
thread1.start();
thread2.start();
thread3.start();
}
}

Output

Thread Thread-1, is Daemon: false


Thread Thread-2, is Daemon: true
Thread Thread-0, is Daemon: false

Example 2

In this example, we've created a ThreadDemo class which extends Thread class. In main
method, we've created three threads. Once a thread starts, it cannot be set as daemon
thread. As we're setting one thread as daemon thread after it started, a runtime
exception will be raised.
Page 4 of 6

We tested this page and blocked content coming from Open Compiler
potentially dangerous or risky sites. Allow this content
only if you're sure it comes from safe sites.
package com.tutorialspoint;
class ThreadDemo extends Thread {
ThreadDemo( ) {
}
public void run() {
System.out.println("Thread " + Thread.currentThread().getName()
+ ", is Daemon: " + Thread.currentThread().isDaemon());
}
public void start () {
super.start();
}
}
public class TestThread {
public static void main(String args[]) {
ThreadDemo thread1 = new ThreadDemo();
ThreadDemo thread2 = new ThreadDemo();
ThreadDemo thread3 = new ThreadDemo();
thread1.start();
thread2.start();
thread3.start();
thread3.setDaemon(true);
}
}

Output

Exception in thread "main" Thread Thread-1, is Daemon: false


Thread Thread-2, is Daemon: false
Thread Thread-0, is Daemon: false
java.lang.IllegalThreadStateException
at java.lang.Thread.setDaemon(Unknown Source)
at com.tutorialspoint.TestThread.main(TestThread.java:27)

TOP TUTORIALS

Python Tutorial
Page 5 of 6

Java Tutorial We tested this page and blocked content coming from
C++ Tutorial potentially dangerous or risky sites. Allow this content
only if you're sure it comes from safe sites.
C Programming Tutorial
C# Tutorial

PHP Tutorial

R Tutorial
HTML Chapters
Tutorial Categories

CSS Tutorial

JavaScript Tutorial
SQL Tutorial

TRENDING TECHNOLOGIES

Cloud Computing Tutorial

Amazon Web Services Tutorial

Microsoft Azure Tutorial


Git Tutorial

Ethical Hacking Tutorial

Docker Tutorial
Kubernetes Tutorial

DSA Tutorial

Spring Boot Tutorial


SDLC Tutorial

Unix Tutorial

CERTIFICATIONS

Business Analytics Certification

Java & Spring Boot Advanced Certification


Data Science Advanced Certification

Cloud Computing And DevOps

Advanced Certification In Business Analytics


Artificial Intelligence And Machine Learning

DevOps Certification

Game Development Certification


Front-End Developer Certification

AWS Certification Training

Python Programming Certification

You might also like