Results 1 to 13 of 13
  1. #1
    gatzke is offline Member
    Join Date
    Jan 2009
    Posts
    11
    Rep Power
    0

    Default Process still running

    I'm working on a menu for a school project which has an "exit" option and the menu disappears. I'm using "System.exit()" to quit the menu. But I noticed that the process (in task manager) is still running after the menu exits. Is there a way to make the process end correctly. There's quite a bit of code, so I'll avoid posting it if possible.

    Cheers.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    exit() not guaranteed that all the process are exited. If you are using it in the main thread, it'll kill all threads related to. But some threads, we call daemon threads, like garbage collector can still running. It's depends on your application implementation.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I thought that it's the other way around. System.exit() will not exit the program if there are non-daemon threads running (or perhaps I misinterpreted your answer).

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  5. #5
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    System.exit() initiates shutdown of the JVM. It doesn't matter what daemon/non-daemon threads a running: if shutdown of the JVM proceeds normally, they will all be killed.

    However, you could conceivably delay/impede normal shutdown of the JVM in one or two subtle ways, for example causing deadlock inside a shutdown hook.

  6. #6
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default from java doc

    The virtual machine's shutdown sequence consists of two phases.
    • In the first phase all registered shutdown hooks, if any, are started in some unspecified order and allowed to run concurrently until they finish.
    • In the second phase all uninvoked finalizers are run if finalization-on-exit has been enabled. Once this is done the virtual machine halts.


    If this method is invoked after the virtual machine has begun its shutdown sequence then if shutdown hooks are being run this method will block indefinitely. If shutdown hooks have already been run and on-exit finalization has been enabled then this method halts the virtual machine with the given status code if the status is nonzero; otherwise, it blocks indefinitely.

    Runtime (Java 2 Platform SE 5.0))
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by neilcoffey View Post
    System.exit() initiates shutdown of the JVM. It doesn't matter what daemon/non-daemon threads a running: if shutdown of the JVM proceeds normally, they will all be killed.

    However, you could conceivably delay/impede normal shutdown of the JVM in one or two subtle ways, for example causing deadlock inside a shutdown hook.
    Correct me if I'm wrong.

    exit() cause to close the JVM, that's right and that's what I know. My question is once the JVM class all the threads including daemon threads are killed? As far as I know daemon thread like garbage collection and stuff hold the system process.

  8. #8
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    No, when the JVM actually shuts down, all threads will be killed, daemon or otherwise.

    However, between the moment of you calling System.exit() and the JVM actually shutting down (i.e. saying to the OS "please kill my process"), there are some intervening cleanup processes where you could potentially stall shutdown.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yep, I was wrong; Neil is right. Thanks for the clarification. I was thinking of what will happen when all non-Daemon threads stop. Then (I believe) the program will end even if daemon threads are running. Corrections welcome.

  10. #10
    lightstream is offline Member
    Join Date
    Jan 2009
    Posts
    9
    Rep Power
    0

    Default

    I use System.exit() in the handler for an Exit file menu item, and it seems to shut down the JVM fine.

    If I use the cross button in the top right of the window though, the JVM appears to remain running indefinitely. How can my code handle those events?

    Thanks v much

  11. #11
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    You need to arrange for System.exit() to be called. In a simple program, you can call JFrame's setDefaultCloseOperation() methd, passing in EXIT_ON_CLOSE. In a more complex application, you may want to add a window listener to trap the close event and do extra cleanup etc before calling System.exit() manually.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by neilcoffey View Post
    No, when the JVM actually shuts down, all threads will be killed, daemon or otherwise.

    However, between the moment of you calling System.exit() and the JVM actually shutting down (i.e. saying to the OS "please kill my process"), there are some intervening cleanup processes where you could potentially stall shutdown.
    Yes neli, I was wrong too. Thanks a lot for the clarification here. I thought that daemon threads can be process without the VM, and by the system.

  13. #13
    lightstream is offline Member
    Join Date
    Jan 2009
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by neilcoffey View Post
    You need to arrange for System.exit() to be called. In a simple program, you can call JFrame's setDefaultCloseOperation() methd, passing in EXIT_ON_CLOSE.
    that's great thanks, works perfectly.

Similar Threads

  1. Process running or not
    By smartsubroto in forum New To Java
    Replies: 4
    Last Post: 11-16-2009, 03:09 AM
  2. urgent requirement...regarding process
    By LakshmiSireesha in forum New To Java
    Replies: 2
    Last Post: 01-15-2009, 03:55 PM
  3. killing a java process
    By paritoshcg in forum Advanced Java
    Replies: 3
    Last Post: 12-01-2008, 08:16 AM
  4. Process exit value = 128
    By trivektor in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-19-2008, 08:11 AM
  5. i need help in http process
    By sureshjava in forum New To Java
    Replies: 0
    Last Post: 01-29-2008, 04:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •