【问题标题】:Thread.stop() is deprecated. But how to use TimerTask instead?Thread.stop() 已弃用。但是如何改用 TimerTask 呢?
【发布时间】:2016-12-09 06:39:55
【问题描述】:
我在按下切换按钮时启动了一个线程。现在我想在再次按下该切换按钮时停止该线程。但是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();
【问题讨论】:
标签:
android
multithreading
runnable
timertask
java-threads
【解决方案1】:
您不能简单地在执行过程中停止线程。如果要在执行过程中停止线程,可以调用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;
}
}
【解决方案2】:
创建一个标志并根据标志停止
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; }
}
现在开始和停止
Server myServer = new Server();
Thread t1 = new Thread(myServer, "T1");
t1.start();
//Now, let's stop our Server thread
System.out.println(currentThread().getName() + " is stopping Server thread"); myServer.stop();
【解决方案3】:
你可以试试这次任务示例;
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;
}
};
}
}
【解决方案4】:
你不能自己停止runnable或者不能停止一个线程你可以通过调用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();