Daemon Thread : Thread Status « Threads « Java






Daemon Thread

Daemon Thread
 
public class DaemonThread implements Runnable {
  public void run() {
    System.out.println("entering run()");

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

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

        System.out.println("in run(): woke up again");
      }
    } finally {
      System.out.println("leaving run()");
    }
  }
  public static void main(String[] args) {
    System.out.println("entering main()");

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

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

    System.out.println("leaving main()");
  }

}


           
         
  








Related examples in the same category

1.Is thread aliveIs thread alive
2.Thread sleepThread sleep
3.Another way to stop a threadAnother way to stop a thread
4.Another way to suspend and resumeAnother way to suspend and resume
5.Visual suspend and resumeVisual suspend and resume
6.Thread sleep and interruptThread sleep and interrupt
7.Pausing the Current Thread: a thread can temporarily stop execution.
8.Pausing a Thread: set a variable that the thread checks occasionally, call Object.wait()
9.set Uncaught Exception Handler
10.Monitor a thread's status.
11.Pause the execution
12.Interrupt a thread.
13.Stopping a Thread: set a variable that the thread checks occasionally
14.Determining When a Thread Has Finished
15.Add a delay
16.Pause the execution of a thread using sleep()