Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Daemon Threads

This section describes about the daemon threads in java.

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

     

In Java, any thread can be a Daemon thread. Daemon threads are like a service providers for other threads or objects running in the same process as the daemon thread. Daemon threads are used for background supporting tasks and are only needed while normal threads are executing. If normal threads are not running and remaining threads are daemon threads then the interpreter exits.

   setDaemon(true/false) ? This method is used to specify that a thread is daemon thread.

   public boolean isDaemon() ? This method is used to determine the thread is daemon thread or not.

The following program demonstrates the Daemon Thread:

public class DaemonThread extends Thread {
  public void run() {
  System.out.println("Entering run method");

  try {
  System.out.println("In run Method: currentThread() is"
  + Thread.currentThread());

  while (true) {
  try {
  Thread.sleep(500);
  catch (InterruptedException x) {
  }

  System.out.println("In run method: woke up again");
  }
  finally {
  System.out.println("Leaving run Method");
  }
  }
  public static void main(String[] args) {
  System.out.println("Entering main Method");

  DaemonThread t = new DaemonThread();
  t.setDaemon(true);
  t.start();

  try {
  Thread.sleep(3000);
  catch (InterruptedException x) {
  }

  System.out.println("Leaving main method");
  }

}

Output of this program is:

C:\j2se6\thread>javac DaemonThread.java

C:\j2se6\thread>java DaemonThread
Entering main Method
Entering run method
In run Method: currentThread() isThread[Thread-0,5,main]
In run method: woke up again
In run method: woke up again
In run method: woke up again
In run method: woke up again
In run method: woke up again
In run method: woke up again
Leaving main method

C:\j2se6\thread>

Download this example

Advertisements

Ads
Share on Google+Share on Google+

Daemon Threads

Posted on: April 17, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Daemon Threads   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
Lakshmi narayana
September 23, 2011
i want to lern java from you

its more helpful for us
shahid
April 9, 2012
how to handle global exeption

package com.lara; public class GlobalExeption { int i=10/0; public static void main(String[] args) { GlobalExeption g=new GlobalExeption(); System.out.println(); }

Ads

 

Ads