How to create daemon thread?

Discussion in 'Mac Programming' started by stevejobbers, Jul 13, 2009.

  1. macrumors member

    Joined:
    Aug 19, 2008
    #1
    I wrote a simple Java program that I'd like to run when I login and have it continue to run in the background (it's a server for a simple client/server program).

    Currently I have a 'command' file that I'm using to start the process, but I have to keep doing it manually and it opens a Terminal window on the dock. I'm hoping there is a way to configure it to run in the background automatically when I log in.

    Thanks guys!!!
     
  2. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    You might want to look into Launchd. Although I am not sure if it supports Java, I see no reason why it wouldn't but do not know for sure.
     
  3. macrumors 6502a

    Joined:
    Oct 13, 2007
    #3
    Cromulent is correct, using a Launchd agent is the right way of going.

    And just a note: you want a daemon process, not thread. Threads aren't really daemons (python has "daemon threads", but that is a different concept).
     
  4. thread starter macrumors member

    Joined:
    Aug 19, 2008
    #4
    thanks, i'll give it a shot.

    is there a good resource online or tutorial? i tried some basic google searches but got kinda lost...
     
  5. macrumors member

    Joined:
    Sep 21, 2008
    #5
    You might want to check Bruce Eckel's free on-line (and downloadable) book Thinking in Java. If I remember correctly, there's a couple of examples.

    What you should basically consider doing thin infinite loop:

    done = false;
    while (!done) {
    .....
    //update done if necessary
    }

    then inside the loop, use a socket to hear for requests on a specific port, and when you get a request, create a new thread to service that request. Main thread keeps listening for new incoming requests.

    Then use launchd to invoke this program via java "YourProgram"

    Hope that helps.
     
  6. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #6
    A launchd server process should quit when not in use so that infinite loop breaks best practice policy. Launchd handles the loading and unloading of server processes as and when they are required to conserve memory and CPU resources.
     
  7. thread starter macrumors member

    Joined:
    Aug 19, 2008
    #7
    thanks for all the responses. the java program already runs in a loop, so that's not an issue. the only issue is trying to get the program to automatically start when i log in, and run in the background. everytime i google for launchd it says that i'll have to create plist files and other stuff.... i thought all i would have to do is add a command to some login script somewhere.

    regardless, i found an application, Lingon, that seems to have worked for me. the process started up at login and continues to run in the background.

    it's a free app, in case anyone else wants/needs to use it.
     
  8. macrumors member

    Joined:
    Sep 21, 2008
    #8
    Very true. Just providing a quick and dirty solution :eek::eek:
     

Share This Page