Avatar of akrishnan051600
akrishnan051600

asked on 

How to create a Daemon thread

I tried to create a daemon thread from main by calling the setDaemon(true) and then calling start. The thread did not start. How do you create a Daemon Thread.
Java

Avatar of undefined
Last Comment
akrishnan051600
Avatar of ovidiucraciun
ovidiucraciun
Flag of United States of America image

create a new thread from your main thread and use setDaemon(true) method...

if you are in nt environment you can use the javaw to launch you app without console, you'll be able to stop your app only from Windows NT Task Manager

if you are on UNIX platform you can launch you app with an & after the name of the app and you'll be able to stop it only with kill command

the use of the Windows NT Task Manager or kill method to stop your application was mentioned for
servers/daemons that are build to run infinitly
Avatar of zicai
zicai

Hi, I had played with daemon thread and didn't find any problem.
What I did is the same as you...

//Create the thread class
public class MyDaemon extends Thread {
    //define some variables
      ....
    //the constructor
    public MyDaemon(String id,...) {
          super(id);
          ....
    }

    //the run method
    public void run() {
          //do the things you want
    }

    //any methods needed..
    .....
}

//declare a thread of type MyDaemon
private MyDaemon myDaemon;

//Start and stop the thread ..
public void startMyDaemon() {
    if(myDaemon==null) {
        myDaemon = new MyDaemon("1",...);//"1" is the thread id
        myDaemon.setDaemon(true);
        myDaemon.start();
    }
}

//Stop the thread..
public void stopMyDaemon() {
    if(myDaemon!=null) {
        myDaemon.stop();
        myDaemon = null;
    }
}


Should work for you if nothing's gone mad:)

Good luck!
Avatar of Jim Cakalic
Jim Cakalic
Flag of United States of America image

How do you check that the thread did not start? The start method doesn't necessarily cause the new thread to begin execution. It only schedules it for execution. Typically, and this is JVM dependent, any code following Thread.start() in the thread performing the start will continue to be executed. This may make it appear, for an indeterminate period of time, that the other thread is not running.

Are you sure that the daemon thread didn't start and run to completion before you checked? The run method of the Runnable or Thread object called by the JVM controls the lifetime of the thread. When the run method finishes, so does the thread. Check that your run method does not exit.

Best regards,
Jim Cakalic
Avatar of vikramb
vikramb

In Java, I don't think there is a way to check whether a scheduled Thread has started or not. Correct me if I'm wrong.
Avatar of akrishnan051600

ASKER

Thanks for all the answers. I had confused a Daemon thread with a Daemon process in Unix. Basically if I am not mistaken the JVM exits when the only threads are Daemon threads. Once the JVM exits there is no way by which the thread can continue execution. If I am mistaken please correct me. I have a doubt. Could someone give me an example of where a Daemon thread can be used in an application(other than GC).
ASKER CERTIFIED SOLUTION
Avatar of terajiv
terajiv

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of akrishnan051600

ASKER

Hi Rajiv,
   I tried what you said. But an IllegalThreadState exception is thrown. In fact if what I remember is right the documentation says that the setDaemon should be called before starting the thread. I used Java 1.2.2 and Windows NT.

Krishnan
Avatar of terajiv
terajiv

Just check it whether u have written setDaemon(true); in constructor of the class or not... if not add it to constructor...

Rajiv
Avatar of akrishnan051600

ASKER

Hi Rajiv,
  You have asked me to put the setDaemon(true) in the constructor. This implies that it will be called before the thread is started. But in your first message you asked me to call it after starting the thread. Could you please explain.

Thanks

Krishnan.
Avatar of akrishnan051600

ASKER

Hi Rajiv,
  You have asked me to put the setDaemon(true) in the constructor. This implies that it will be called before the thread is started. But in your first message you asked me to call it after starting the thread. Could you please explain.

Thanks

Krishnan.
Avatar of akrishnan051600

ASKER

Hi Rajeev,
  You have asked me to put the setDaemon(true) statement in the constructor. This means that it will be called before the thread is started, but in the previous message you asked me to call it after the thread is started. Could you please explain.

Thanks
Krishnan
Avatar of terajiv
terajiv

Yes Yaar I m Sorry for the First Statement... It has to be called before the Thread starts...

Im very very Sorry...
But did u tried that?

Rajiv
Avatar of akrishnan051600

ASKER

Hi Rajeev,
   I tried it. The run of the daemon thread executes if the main thread does not die. On the other hand if the main method just creates a daemon thread and starts it then the run is not executed.

public static void main(String args[])
{
  MyThread t = new MyThread();
   /*This creates a thread and also calls setDaemon(true) */
  t.start();
}

I guess the reason for this is that the JVM exits since it finds the only thread to be a Daemon. Once the JVM has exited there is no way by which the thread can run. Please tell me if what I have understood is correct.

Thanks
Krishnan
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo