how to destroy a thread , pause/suspend a thread , resume/runAgain a thread?

10,172

Solution 1

Thread.stop() is no longer used since it was considered dangerous: http://download.oracle.com/javase/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html.

You must let the thread come naturally to an end as a result of a variable change. The link also gives some advice about how to achieve this.

public class MyThread extends Thread {

    private boolean threadDone = false;

    public void done() {
        threadDone = true;
    }

    public void run() {
        while (!threadDone) {
            // work here
            // modify common data
        }
    }

}

Warning: make sure you either use a guarded block in the looping code, a method that blocks itself, or a Thread.sleep(..). Thread.sleep is the most primitive of these if you don't understand guarded blocks, but it will work. You could also wait forever and use the interrupt mechanism to cancel the thread which is thrown as InterruptedException in the try-catch block when you use a wait or sleep. For this, use !Thread.currentThread().isInterrupted() as the loop guard condition, then use your Thread object and call thread.interrupt().

Solution 2

To control a Java thread, you should add methods to the object that can be called by other objects which set variables read by your run() method. You don't give much information on exactly what you're doing, but here's a possible pattern:

public class ProgressBarUpdater implements Runnable{
    private volatile boolean paused = false;
    private volatile boolean finished = false;

    /* other fields, constructor etc. */

    public void run(){
        while(!finished){
            updateProgressBar();

            while(paused && !finished){
                try{
                    Thread.sleep(1000); //Busy wait - should really use wait/notify, but that's another lesson
                }
                catch(InterruptedException e){

                }
            }
        }
    }

    public synchronized void pauseProgressBar(){
        paused = true;
    }

    public synchronized void unPauseProgressBar(){
        paused = false;
        //call notify() here when you switch to wait/notify.
    }

    public void stopProgressBar(){
        finished = true;
        //call notify() here too.
    }
}

You will probably want to use more robust synchronisation around the control variables, and, as mentioned in the comments, wait/notify rather than a busy wait.

Use as so:

ProgressBarUpdater pbu = new ProgressBarUpdater();

Thread t = new Thread(pbu);

t.start();
Thread.sleep(10000); //let the progress bar run for ten seconds.

pbu.pauseProgressBar();
Thread.sleep(10000); //pause it for ten seconds.

pbu.unPauseProgressBar();
Thread.sleep(10000); //restart for another ten seconds.

pbu.stopProgressBar(); //stop progress bar.

Solution 3

You have a few options and they depend on how you define the various states of your thread.

A thread is effectively stoped when it exits the run() method.

To "pause" and "resume" a thread's execution you can can use wait() and notify().

To illustrate this, here's a quick example:

class MyThread implements Runnable {
    private boolean keepRunning = false;
    private boolean isPaused = false;

    public void run() {
        keepRunning = true;
        try {
            while (keepRunning) {
                // do stuff here
                if (isPaused) {
                    synchronized (this) {
                        // wait for resume() to be called
                        wait();
                        isPaused = false;
                    }
                }
            }
        } catch (Exception ex) {
            // do stuff
        }
    }

    // note that as-is this won't do anything to a paused thread until
    // it is resumed.
    public void stop() {
        keepRunning = false;

    }

    public void pause() {
        isPaused = true;
    }

    public synchronized void resume() {
        // notify anybody waiting on "this"
        notify();
    }
}
Share:
10,172
abhishek
Author by

abhishek

Updated on June 17, 2022

Comments

  • abhishek
    abhishek almost 2 years

    Hey guys I am using runnable outside the oncreate in my android application where i have used thread to setprogress of ProgressBar. What i dont know is how to stop/destry the thread when stop button is pressed since thread.stop is not a method and how to resume from that , how to even destroy the thread.

    I know i have to make some methods and members in runnable but i dont exactly know what??