Hi
I have a class which is using ThreadPoolExecutor to submit tasks/Runnables. The task runs fine.
The task/Runnables are submitted with a unique ID.
Que:- If the I want to cancel a specific task (say 3rd task) how can I do that.
Here is the somewhat modified code snippet from my project.
I have a class which is using ThreadPoolExecutor to submit tasks/Runnables. The task runs fine.
The task/Runnables are submitted with a unique ID.
Que:- If the I want to cancel a specific task (say 3rd task) how can I do that.
Here is the somewhat modified code snippet from my project.
Code:
class Task implements Runnalbe {
private String mTaskId;
public Task(String id) { mTaskId = id; }
@Override
public void run(){
int i = 100;
int sleepTime = 500;
while(i != 0){
Logging.i(TAG, "###XXX--- TaskID " + mTaskId + " countDown " + i);
--i;
try{
Thread.sleep(sleepTime);
}catch (InterruptedException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public String toString(){
return mTaskId;
}
}
/*Class to manage the task*/
class TaskManager{
private ThreadPoolExecutor mThreadPoolExecutor;
private ArrayBlockingQueue<Runnable> mArrayBlockingQueue;
private RejectedExecutionHandlerImpl mRejectedExeHandler;
public TaskManager(){
mArrayBlockingQueue = new ArrayBlockingQueue<Runnable>(MAX_QUEUE_SIZE);
mRejectedExeHandler = new RejectedExecutionHandlerImpl();
mThreadPoolExecutor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS, mArrayBlockingQueue,
mRejectedExeHandler);
}
public boolean addToQueue(Task task){
mThreadPoolExecutor.submit(downloadTask);
}
public void cancelTask(String taskId){
// ???????????????????????????????????
}
}
/* Task adding */
TaskManager taskManager = new TaskManger();
Task task;
for(int i = 0; i < 5; i++)
{
task = new Task(String.valueOf(i));
taskManager.addToQueue(task);
}
taskManager.cancelTask(String.valueOf(3));