Daemon Threads

This section describe about daemon thread in java. Any thread can be a daemon thread.

Ads

Tutorials   
Threading In Java Example Thread Life Cycle Example in java What Is Thread In Java? Creating multiple Threads Daemon Threads Synchronization in java with example Thread priority in java Threads in Java Java Multithreading Example Thread Synchronization in Java Overview of Networking through JAVA,How to retrieve URL information URL in term of Java Network Programming Overview of Networking through JAVA,Convert URI to URL Socket and ports Server Sockets Overview of Networking through JAVA,Find the Host name in reverse of given IP address Print the URL of a URLConnection Networking in Java Computer Networking Overview of Networking through JAVA,Getting the Local port Overview of Networking through JAVA Overview of Networking through JAVA Overview of Networking through JAVA,Getting list of Local Interfaces on a machine Overview of Networking through JAVA Overview of Networking through JAVA,Find Your Host Name Overview of Networking through JAVA,To retrieve the IP address from Host Name, vice-versa Open a URLConnection to specific website address Return the MIME Header Construct a DatagramSocket on an unspecified port Datagram in network environment Construct a DatagramPacket to receive data Client-Server Architecture Thread Priorities Synchronized Threads Multithreading in Java Interthread Communication Creation of MultiThreads Threading in Java Thread Priorities Inter-Thread Communication Synchronized Threads Creation of Multiple Threads Threading in Java Daemon Threads Deadlock Synchronization Life Cycle of Threads Thread Constructors Thread Creation Overview of Thread
Ads

Daemon Threads

This section describe about daemon thread in java. Any thread can be a daemon thread. Daemon thread are service provider for other thread running in same process, these threads are created by JVM for background task. Any thread created by main thread that run the main method is by default user thread because it inherit the daemon nature from its parent thread from which it is created. Any thread which is created by user thread is user or non daemon thread only, until setDaemon(true) method is true. Thread.setDaemon(true) will create a daemon thread but it can be called only before thread is started or it will throw IllegalThreadStateException if thread is already started and running. The java.lang.Thread class provide two method for daemon thread as follows :

  • public void setDaemon(boolean value) - used to make the user thread as daemon thread.
  • public boolean isDaemon() -  To check whether the thread is daemon thread or not.

Main purpose of daemon thread is to provide services for background task of  user thread if there is not any user thread then JVM will terminate the daemon thread.

Example : A code for creating Daemon Thread

public class DaemonThread  extends Thread
   {
  public void run()
  {
	System.out.println(Thread.currentThread().getName());
	System.out.println(Thread.currentThread().isDaemon());
   }
   
   public static void main(String args[])
   {
	   DaemonThread ob1=new DaemonThread ();
	   DaemonThread ob2=new DaemonThread ();
	   ob1.setDaemon(true);
	   ob1.start();
       ob2.start();
       //ob2.setDaemon(true);// Not allowed it will throw an exception.
     }         
   }

If a user thread is set true to setDaemon() method for creating daemon thread, then it will only allow before thread is started or else it will result an error.

Output : After compiling and executing the above program

Download SourceCode

Advertisements

Ads
Share on Google+Share on Google+

Daemon Threads

Posted on: May 22, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Daemon Threads  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads