A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/java/java_daemon_thread.htm below:

Java - Daemon Thread

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 Thread Class Methods for Java Daemon Thread

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

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, no thread is marked as daemon thread.

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.

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.

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)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4