不推荐使用Thread.stop()。但是如何使用TimerTask呢?

时间:2016-12-09 06:39:56

标签: android multithreading runnable timertask java-threads

按下切换按钮后,我开始了线程。现在我想再次按下切换按钮停止该线程。但Thread.stop() API已被弃用。所以,我得到UnsupportedOperationException。我不知道如何使用 TimerTask 作为替代方案。这是我的示例代码:

//AudioDispatcher implements a Runnable
public class AudioDispatcher implements Runnable

//This is a code to start a thread
AudioDispatcher dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(22050,1024,0);
Thread t1 = new Thread(dispatcher,"Audio Dispatcher");
t1.start();

4 个答案:

答案 0 :(得分:2)

您无法在执行中期停止线程。如果你想在执行中期停止一个线程,那么你可以调用Thread.interrupt()方法。

public class SomeBackgroundProcess implements Runnable {

Thread backgroundThread;

public void start() {
   if( backgroundThread == null ) {
      backgroundThread = new Thread( this );
      backgroundThread.start();
   }
}

public void stop() {
   if( backgroundThread != null ) {
      backgroundThread.interrupt();
   }
}

public void run() {
    try {
       Log.i("Thread starting.");
       while( !backgroundThread.interrupted() ) {
          doSomething();
       }
       Log.i("Thread stopping.");
    } catch( InterruptedException ex ) {
       // important you respond to the InterruptedException and stop processing 
       // when its thrown!  Notice this is outside the while loop.
       Log.i("Thread shutting down as it was requested to stop.");
    } finally {
       backgroundThread = null;
    }
}

答案 1 :(得分:0)

您可以尝试这次任务样本;

private static final int DEFAULT_DELAY_TIME = 1000;
private static final int DEFAULT_PERIOD = 1000;
private Timer mPlayTimer;
private TimeTask mPlayPlanTask;

private void startTask() {
        configTimer();
        mPlayTimer.schedule(mPlayPlanTask, DEFAULT_DELAY_TIME,
              DEFAULT_PERIOD);
    }
}

private void stopTimer() {
    if (mPlayTimer != null) {
        mPlayTimer.cancel();
        mPlayTimer = null;
    }
    if (null != mPlayPlanTask) {
        mPlayPlanTask.cancel();
        mPlayPlanTask = null;
    }
}

private void configTimer() {
    if (null == mPlayTimer) {
        mPlayTimer = new Timer();
    }
    if (null == mPlayPlanTask) {
        mPlayPlanTask = new TimerTask() {
            @Override
            public void run() {
                  // Do some work;
            }
        };
    }
}

答案 2 :(得分:0)

创建一个标志并根据标志

停止
<html>
<head>
<style>
[title~='77'] {
    border: 5px solid yellow;
}
</style>
</head>
<body>
  <a title="One Piece vol 77 GN"> one piece vol 77</a>
  <a title="One Piece vol 78 GN"> one piece vol 78</a>
  <a title="Naruto vol 77 GN"> naruto vol 77</a>
</body>
</html>

立即开始和停止

 class Server implements Runnable{
      private volatile boolean exit = false;

      public void run() {

     while(!exit){ 
        System.out.println("Server is running....."); 
    } 

    System.out.println("Server is stopped...."); 
    } 

    public void stop(){ exit = true; } 
}

答案 3 :(得分:0)

你不能停止自己运行,或者不能通过调用Thread.interrupt()来阻止你可以打断线程的线程。如果您需要释放一些资源,请将AudioDispatcher扩展为Thread而不是Runnable覆盖interrupt()

public class AudioDispatcher extends Thread {

    @Override
    public void run() {
        // check if thread is interrupted do nothing
        if(!Thread.currentThread().isInterrupted()){

        }
    }

    @Override
    public void interrupt() {
        super.interrupt();
        // release resources if any
    }
}

现在通过

开始你的主题
AudioDispatcher audioDispatcherThread = new AudioDispatcher();
audioDispatcherThread.start();

当您需要停止线程调用时audioDispatcherThread.interrupt();