Results 1 to 4 of 4
  1. #1
    rajkobie is offline Member
    Join Date
    Nov 2010
    Posts
    54
    Rep Power
    0

    Default What is a replacement for Thread.stop( )?

    I dont have a thread in my program, but my computer genius friend told me that every Java application is like a thread. He was kind of right because even though i dont have a Thread i use Thread.sleep ( ) to reduce lag. However, i need to stop the Thread when my program is over. He said that i should not use the Thread.stop ( ) method. What is a replacement for that method where i can stop the execution of the program?

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can use System.exit(0); to exit the program. Everything in java is a thread, the main method starts the main thread which does stuff and makes new threads. As far as I am aware you can mostly be blissfully ignorant about threads until you are getting started on using threads.

    Also, you don't really need to explicitly end the program, it will end when it reaches the closing curly brace and shouldn't cause any problems.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,334
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by sunde887 View Post
    Also, you don't really need to explicitly end the program, it will end when it reaches the closing curly brace and shouldn't cause any problems.
    Only when no other non-daemon Threads are running. The only difference between a daemon Thread and a non-daemon Thread is when the last non-daemon Thread dies: the daemon Threads also die with it. You can set the daemon status of a Thread before you start it (as always: read the API documentation).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    If your main thread starts other threads and you want to cleanly stop them, don't use System.exit(). Call interrupt() on your other threads. Calling interrupt() breaks them out of any blocking I/O and sets their interrupted flag. They should check Thread.currentThread().isInterrupted() in their main loop and exit if it's true.

Similar Threads

  1. Thread help - how to stop?
    By scheffetz in forum New To Java
    Replies: 3
    Last Post: 04-10-2011, 01:14 PM
  2. How to stop Thread
    By ersachinjain in forum Threads and Synchronization
    Replies: 2
    Last Post: 11-30-2009, 07:11 PM
  3. Thread won't stop
    By bubbless in forum Threads and Synchronization
    Replies: 15
    Last Post: 10-20-2009, 10:58 PM
  4. Help to stop a thread
    By raghu_lzybns in forum New To Java
    Replies: 4
    Last Post: 07-09-2009, 04:39 PM
  5. how to stop a thread
    By willemjav in forum Advanced Java
    Replies: 19
    Last Post: 09-10-2008, 07:11 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
  •