如何销毁一个线程,暂停/挂起一个线程,恢复/运行一个线程?

时间:2011-03-21 20:55:22

标签: java android multithreading progress-bar media-player

嘿伙计们 我在我的android应用程序中使用oncreate外部运行runnable,其中我使用了线程来设置ProgressBar的进度。我不知道如何在按下停止按钮时停止/退出线程,因为thread.stop不是一种方法,如何从中恢复,如何甚至破坏线程。

我知道我必须在runnable中制作一些方法和成员,但我不知道是什么?

4 个答案:

答案 0 :(得分:3)

Thread.stop()已不再使用,因为它被认为是危险的:http://download.oracle.com/javase/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html

由于变量的变化,你必须让线程自然而然地结束。该链接还提供了有关如何实现此目的的一些建议。

public class MyThread extends Thread {

    private boolean threadDone = false;

    public void done() {
        threadDone = true;
    }

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

}

警告:请确保在循环代码中使用guarded block,阻止自身的方法或Thread.sleep(..)。如果您不了解受保护的块,Thread.sleep是最原始的,但它会起作用。您还可以永远等待并使用中断机制取消在使用等待或休眠时在try-catch块中作为InterruptedException抛出的线程。为此,使用!Thread.currentThread().isInterrupted()作为循环保护条件,然后使用Thread对象并调用thread.interrupt()

答案 1 :(得分:2)

您有几个选项,它们取决于您如何定义线程的各种状态。

当线程退出run()方法时,它会被有效地停止。

要“暂停”并“恢复”线程的执行,您可以使用wait()和notify()。

为了说明这一点,这是一个简单的例子:

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();
    }
}

答案 2 :(得分:2)

要控制Java线程,您应该向对象添加方法,这些方法可以由设置run()方法读取的变量的其他对象调用。您没有提供有关您正在做什么的详细信息,但这是一种可能的模式:

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.
    }
}

您可能希望在控制变量周围使用更强大的同步,并且如评论中所述,等待/通知而不是忙碌的等待。

如此使用:

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.

答案 3 :(得分:0)

让其他线程定期检查布尔标志(isCancelled或类似的东西)。最初是假的。

从停止按钮代码中,将此值设置为true。

当你的线程接下来检查该标志并发现它为真时,该线程应该自行终止。