Android 停止在threadpoolexecutor中运行任务的最佳方法是什么?
我正在Android中实现Java ThreadPoolExecutor。我需要停止并从我的池中删除正在运行的任务 我使用submit(Runnable)和Future.cancel()方法实现了这一点 提交任务的代码如下:Android 停止在threadpoolexecutor中运行任务的最佳方法是什么?,android,threadpoolexecutor,Android,Threadpoolexecutor,我正在Android中实现Java ThreadPoolExecutor。我需要停止并从我的池中删除正在运行的任务 我使用submit(Runnable)和Future.cancel()方法实现了这一点 提交任务的代码如下: public Future<?> submitTask(Runnable runnableTask) throws CustomException { if (runnableTask == null) { throw new Custo
public Future<?> submitTask(Runnable runnableTask) throws CustomException {
if (runnableTask == null) {
throw new CustomException("Null RunnableTask.");
}
Future<?> future = threadPoolExecutor.submit(runnableTask);
return future;
}
public void cancelRunningTask(Future<?> future) throws CustomException {
if (future == null) {
throw new CustomException("Null Future<?>.");
}
if (!(future.isDone() || future.isCancelled())) {
if (future.cancel(true))
MyLogger.d(this, "Running task cancelled.");
else
MyLogger.d(this, "Running task cannot be cancelled.");
}
}
public Future submitTask(Runnable runnableTask)抛出CustomException{
if(runnableTask==null){
抛出新的CustomException(“Null RunnableTask”);
}
Future-Future=threadPoolExecutor.submit(runnableTask);
回归未来;
}
submit()返回的Future将传递给下面的方法。
取消任务的代码如下:
public Future<?> submitTask(Runnable runnableTask) throws CustomException {
if (runnableTask == null) {
throw new CustomException("Null RunnableTask.");
}
Future<?> future = threadPoolExecutor.submit(runnableTask);
return future;
}
public void cancelRunningTask(Future<?> future) throws CustomException {
if (future == null) {
throw new CustomException("Null Future<?>.");
}
if (!(future.isDone() || future.isCancelled())) {
if (future.cancel(true))
MyLogger.d(this, "Running task cancelled.");
else
MyLogger.d(this, "Running task cannot be cancelled.");
}
}
public void cancelRunningTask(未来)引发CustomException{
if(future==null){
抛出新的CustomException(“空未来”);
}
if(!(future.isDone()| future.isCancelled()){
if(future.cancel(true))
d(这是“正在运行的任务已取消”);
其他的
MyLogger.d(这是“无法取消正在运行的任务”);
}
}
问题:任务实际上没有取消。请让我知道我错在哪里。如有任何帮助,我们将不胜感激。请参阅有关未来任务的说明。据我所知,如果执行开始,我们不能取消它。然后我们可以做什么来获得取消的效果是中断正在运行未来任务的线程
mayInterruptIfRunning - true
在您的runnable中,在不同的位置,您需要检查线程是否被中断,如果被中断则返回,这样只有我们可以取消它
Thread.isInterrupted()
样本:
private Runnable ExecutorRunnable = new Runnable() {
@Override
public void run() {
// Before coming to this run method only, the cancel method has
// direct grip. like if cancelled, it will avoid calling the run
// method.
// Do some Operation...
// Checking for thread interruption
if (Thread.currentThread().isInterrupted()) {
// Means you have called Cancel with true. So either raise an
// exception or simple return.
}
// Do some Operation...
// Again Checking for thread interruption
if (Thread.currentThread().isInterrupted()) {
// Means you have called Cancel with true. So either raise an
// exception or simple return.
}
// Similarly you need to check for interruption status at various
// points
}
};
你确定你的任务没有因为已经完成而被取消吗?@42:是的,我他妈的确定!!谢谢你宝贵的回答。但是Future.cancel()方法有什么用呢?来自Android API文档:每当你向线程池执行器提交一个新任务时,如果没有空闲线程来执行任务,它实际上会进入一个内部队列。因此,如果您的cancel调用在任务开始执行之前,它将被取消。但是这段代码只会使线程的中断状态变为true。因此,在runnable中,我们需要检查线程是否被中断,并相应地执行操作。例如:假设您在可运行代码中执行某些IO操作,并且如果您使用true调用cancel方法,则会设置线程中断状态,并且在android的IO处理代码中,他们正在检查是否中断,如果为true,则会引发ThreadInterrupted异常。在我们的普通代码中,我们也需要这样做,否则就不用“cancel(true)”了。非常感谢您把这一点讲清楚。我接受你的回答。我没有必要的声誉,否则我会投票赞成你的答案:)。