CodesJava

Easy learning with example program codes

Daemon thread in java


Daemon thread:

Daemon threads are low priority threads which are act as a service provider for user threads. Life of a daemon thread is depends upon the user threads. JVM automatically terminates daemon thread when all user threads are died. Daemon threads are used for background supporting tasks.

Methods used for daemon threads:

1. public final void setDaemon(boolean on)

Marks this thread as daemon thread if on is true.

2. public final boolean isDaemon()

Returns true if thread is daemon.

 Example:

DaemonThreadExample1.java

/**
 * This program is used to show daemon thread example.
 * @author codesjava
 */
class Test extends Thread{
	public void run(){
	    System.out.println(Thread.currentThread().getName() + " " 
			+ Thread.currentThread().isDaemon());
	}
}
 
public class DaemonThreadExample1 {
	public static void main(String args[]){
		//creating thread.
		Test thrd1 = new Test();
		Test thrd2 = new Test();
 
		//set names
		thrd1.setName("My Thread1");
		thrd2.setName("My Thread2");
 
		//set thrd1 as daemon thread.
		thrd1.setDaemon(true);
 
		//start threads.
		thrd1.start();
		thrd2.start();
	}
}

Output:

My Thread1 true
My Thread2 false

Download this example.

Example: After starting a thread it can’t be set as daemon thread.

DaemonThreadExample2.java

/**
 * This program is used to show that after starting a thread
 * it can't be set as daemon thread.
 * @author codesjava
 */
class Test extends Thread{
	public void run(){
	  System.out.println(Thread.currentThread().getName() + " " 
			+ Thread.currentThread().isDaemon());
	}
}
 
public class DaemonThreadExample2 {
	public static void main(String args[]){
		//creating thread.
		Test thrd1 = new Test();
		Test thrd2 = new Test();
 
		//set names
		thrd1.setName("My Thread1");
		thrd2.setName("My Thread2");
 
		//start thrd1.
		thrd1.start();
 
		//set thrd1 as daemon thread.
		//java.lang.IllegalThreadStateException here.
		thrd1.setDaemon(true);
 
		//start thread2.
		thrd2.start();
	}
}

Output:

Exception in thread "main" My Thread1 false
java.lang.IllegalThreadStateException
at java.lang.Thread.setDaemon(Unknown Source)
at com.codesjava.business.DaemonThreadExample2.main
(DaemonThreadExample2.java:30)

Download this example.
 
Next Topic: Can we start a thread twice?
Previous Topic: Joining a thread in java with example.

Related Topics:

Posted in Java   

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
JVM, JRE and JDK.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap