-1

Ive got a question. I write a Java Program which I start as a daemon. So I have a class where I implement Daemon and Runnable. In init() I do some checks and then, when the checks are okay, I start a new thread.

thread = new Thread(this);

If the checks are not okay, I call stop().In stop I have this call:

thread.join();

But how would this possibly work, if I never created the new Thread. I would get a NullPointerException. How should I handle this problem? Catch NullPointerException? Only call thread.join() if thread is not null? Dont call thread.join()? What would be the best way and why? Thank you :-)

The reason I asked this question is just that I never implemented a daemon before and I wasnt sure how to handle threads there. Maybe I shouldnt have asked. Thanks anyway.

nano7
  • 2,455
  • 7
  • 35
  • 52
  • 3
    And you can't do something like: `if (thread != null) thread.join();` ?? Catching `NullPointerException` is certainly _not_ the way to do it. – Gray May 25 '12 at 14:11
  • Didnt I ask this? Look above: "Only call thread.join() if thread is not null?" I just asked to make sure there isnt a specific way how to handle this thread-issue in daemons. But thanks anyway. – nano7 May 25 '12 at 14:16
  • Sorry @nano7. This seemed like a straightforward question and I wanted to make sure I got it right. I've moved my comment to an answer. – Gray May 25 '12 at 14:23

2 Answers2

2

This seems a little like Java 101 answer so I'm not sure I understand the question.

In you init() method, you may or may not be starting a new thread and setting the thread field. In your stop() method, the thread field may be null or not. If you want to join() with that thread only if it is not null then you would use the code:

   if (thread != null) {
      thread.join();
   }

Catching NullPointerException should be avoided. Creating exceptions is actually a rather expensive process -- especially where they take a snapshot of the stack frame. Here's a good discussion which quotes the "Effective Java" book as saying that exceptions are ~70 times slower than non.

How expensive are Exceptions

Many people use exceptions to return status information to the caller but this is a bad pattern IMO. Exceptions are for "exceptional" conditions and do not substitute for error codes and returned objects.

If I'm not understanding the question, please edit your post and I'll adjust my answer.

Community
  • 1
  • 1
Gray
  • 115,027
  • 24
  • 293
  • 354
  • Sorry, that I asked a Java 101 Question. The reason just was that I was not sure how to handle Threads in Daemons, because Ive never implemented a daemon before and I wasnt even sure if I had to call thread.join(). Thats why I asked. Im not surprised by your answer anyway, as I suggested it myself. Im sorry for asking this unnecessary question. Thanks for your help. – nano7 May 25 '12 at 14:50
  • No worry @nano7. Pays to be careful. By daemons to you mean `thread.setDaemon(true)` or something else? – Gray May 25 '12 at 14:51
  • No, sorry, I just meant I wrote a java program which can be started as a daemon by /etc/init.d/daemon-name. The class which is used to start the whole process implements Daemon and Runnable. – nano7 May 29 '12 at 08:42
2

If you extends the Interface Daemon of apache then if init() method fail you have to throw the DeamonInitException. Doing this start on the thread will never be called and the Daemon will abort his execution.

aivaldi
  • 67
  • 2
  • 12
  • in init() I do some checks. If they fail, I do _not_ call thread = new Thread(this), but I call stop(). In stop() I had then the problem that the thread was null. So what happens if instead of calling stop() I would throw a DaemonInitException? Would the program break at this point and nothing more would happen? You are right, I would not have the null-Problem then, but would there be any further advantage of throwing this exception? Thank you :-) – nano7 May 29 '12 at 08:50
  • I now read in the example of apache Daemon that personal initializations should be done in start(). So maybe its not a good thing anyway to do personal inits in init() and to throw DaemonInitException if this did not work? Maybe I should move this code to start() and create the new threads there? – nano7 May 29 '12 at 08:59
  • Ok, anyway you have to check always if thread isn't null. But is important also the States of the Deamon. Here [link]( http://commons.apache.org/daemon/apidocs/org/apache/commons/daemon/Daemon.html), explains the methods. Use Init to initialization, so I think the Thread t = new Thread should be there, and the stop should be at destroy. If you throw the exception then the execution ends and not stop() is ever called. – aivaldi Jun 08 '12 at 13:41