Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Question Stopping a running thread

    How to stop a running thread where the Thread.stop method deprecated,
    is there any process other than raising an exception between the thread process?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,583
    Rep Power
    23

    Default Re: Stopping a running thread

    The running thread could test a flag and exit.

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Stopping a running thread

    Here is an example for you

    Java Code:
        private volatile Thread myThread;
        public void stopMyThread() {
            Thread tmpThread = myThread;
            myThread = null;
            if (tmpThread != null) {
                tmpThread.interrupt();
            }
        }
        public void run() {
            if (myThread == null) {
               return; // stopped before started.
            }
            try {
                // all the run() method's code goes here
                ...
                // do some work
                Thread.yield(); // let another thread have some time perhaps to stop this one.
                if (Thread.currentThread().isInterrupted()) {
                   throw new InterruptedException("Stopped by ifInterruptedStop()");
                }
                // do some more work
                ...
            } catch (Throwable t) {
               // log/handle all errors here
            }
        }
    from: http://forward.com.au/javaProgrammin...opAThread.html

  4. #4
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: Stopping a running thread

    i want to terminate its process before completing its task

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Stopping a running thread

    In the link I gave you it argues that you should stop a thread with the interrupt()

  6. #6
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 02-10-2011, 04:10 AM
  2. stopping the thread
    By kailash in forum Threads and Synchronization
    Replies: 2
    Last Post: 02-06-2011, 01:21 PM
  3. Stopping a thread
    By Arne in forum Threads and Synchronization
    Replies: 9
    Last Post: 10-21-2010, 10:26 AM
  4. Stopping a thread
    By userj2ee in forum New To Java
    Replies: 3
    Last Post: 08-13-2010, 07:57 PM
  5. Replies: 11
    Last Post: 01-26-2009, 12:22 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
  •