Teachnique
      CourseRoadmaps
      Login

      OverviewHistoryFeaturesJava 8 - New Featuresvs C++Virtual Machine(JVM)JDK vs JRE vs JVMHello World ProgramEnvironment SetupBasic SyntaxVariable TypesData TypesType CastingUnicode SystemBasic OperatorsCommentsStreamsNew Date-Time API

      Loop ControlDecision Makingif-else Statementswitch statementfor loopfor each Loopwhile Loopdo...while Loopbreak Statementcontinue Statement

      OOPs (Object-Oriented Programming) ConceptsObject and ClassesClass AttributesClass MethodsMethodsVariable ScopesConstructorsAccess ModifiersInheritanceAggregationPolymorphismOverridingMethod OverloadingDynamic BindingStatic BindingInstance Initializer BlockAbstractionEncapsulationInterfacesPackagesInner classesStatic ClassesAnonymous ClassesSingleton ClassWrapper ClassesEnum Class

      Number ClassBoolean classCharacter ClassArraysMath Class

      File ClassCreating FilesWrite To FilesReading FileDelete FilesDirectory OperationsFiles and I/O

      ExceptionsTry Catch BlockTry with ResourcesMultiple Catch BlocksNested Try BlockFinally BlockThrows and Throw | Throw an ExceptionException PropagationBuilt-in ExceptionsCustom Exception

      MultithreadingThread Life CycleCreating a ThreadStarting a ThreadJoining ThreadsNaming a Thread with ExamplesScheduling Threads with ExamplesThread PoolsMain ThreadThread PriorityDaemon ThreadThreadGroup ClassJVM Shutdown Hook

      Thread SynchronizationBlock SynchronizationStatic SynchronizationInter Thread CommunicationThread DeadlockInterrupting ThreadThread ControlReentrant Monitor

      NetworkingSocket ProgrammingURL ProcessingURL ClassURLConnection ClassHttpURLConnection ClassSocket Class with ExamplesGenerics

      Collections FrameworkCollection Interface

      List InterfaceArrayList Class

      Queue InterfaceArrayDeque Class

      Map InterfaceSortedMap Interface

      Set InterfaceSortedSet Interface

      Data Structures Enumeration Interface BitSet Class

      How to Use Iterator?How to Use Comparator?How to Use Comparable?

      RecursionRegular ExpressionsSerializationString ClassJava Arrays - Class

      Feedback

      Submit request if you have any questions.

      Course
      Daemon Thread

      Java Tutorial

      This Java tutorial is tailored for newcomers, offering a journey from basic principles to complex Java programming techniques. Completing this tutorial equips you with a solid understanding of Java, preparing you for advanced learning. You'll emerge ready to tackle the challenges of becoming a top-tier software engineer, with the skills to innovate and excel in the vast world of software development.

      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, 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)