mercredi 29 juin 2016

Threadpoolexecutor rejecting tasks

I have implemented a threadpoolexecutor with

corepoolsize = 5
maxPoolsize = 8
keep alive time = 5
time unit = milliseconds
queue = new LinkedBlockingQueue()

and a rejectionhandler

I am submitting tasks using

executor.submit(runnable)

I have a listview where i have to perform bitmap operations for each item. So whenever user scrolls down i am submitting each task to executor to execute. Initially for 5 tasks it works fine. Later on the executor starts rejecting tasks even though all the tasks running in executor have finished their tasks. In my case it's very rare that all of the tasks are running and there is no thread to execute incoming tasks. By the time next task is submitted there should be any available thread to execute that task. Even though if that happens queue should hold those tasks. But it's not happening. As i keep submitting tasks it accepts some tasks in between and rejects rest of them.

What am i doing wrong? Should i remove the task as soon as the task is completed?

How do we know if the executor is properly reusing the threads after they have finished executing current operations ?

Below is my code

//keep alive time unit

class BackgroundWorker {


private final long KEEP_ALIVE_TIME = 5;

private final TimeUnit KEEP_ALIVE_TIME_UNIT = TimeUnit.MILLISECONDS;

private final BlockingQueue<Runnable> jobQueue;

private final ThreadPoolExecutor jobExecutor;

private int NUMBER_OF_CORES = 8;

private int INITIAL_THREAD_POOL_SIZE = 5;
private BackgroundWorker() {
jobQueue = new LinkedBlockingQueue<Runnable>();

    jobExecutor = new ThreadPoolExecutor(INITIAL_THREAD_POOL_SIZE, NUMBER_OF_CORES, KEEP_ALIVE_TIME, KEEP_ALIVE_TIME_UNIT, jobQueue, this);

 }
public Future<V> addTasktoExecuteandgetFuture(Callable<V> task){
    return jobExecutor.submit(task);
}

}

class TaskManager {
//
//
private BackgroundWorker mWorker = BackgroundWorker.getInstance();
//
//Task implementes runnable
public void addTask(Task task) { 
    mWorker.addTasktoExecuteandgetFuture(task);
}
}

addTask(Task task) is called multiple times to execute the tasks.

Below pictures can help in identifying the issue

ThreadPoolExecutor during runtime ThreadPoolExecutor during runtime

This also might help This also might help

Aucun commentaire:

Enregistrer un commentaire