I want to increase the performance to great extent of ThreadPoolExecutor

617 Views Asked by At
BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(100, true);
RejectedExecutionHandler handler = new ThreadPoolExecutor.CallerRunsPolicy();
ExecutorService executor = new ThreadPoolExecutor(4, 10, 0L, TimeUnit.MILLISECONDS, queue,handler);

executor.execute(new Runnable() {
                        @Override
                        public void run() {
                        for(looping arraylist){
                           operations related to calling proc...
                         }
                     }
                   });

executor.shutdown();
        while (executor.isTerminated() == false){
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

The performance of the above code is way too slow, it takes 7minutes to complete the entire operation and get the data.

I am trying get large data from 2 stored procedures. To increase the speed I am using multithreading.

Can someone please provide solution to increase the performance to large extent.

1

There are 1 best solutions below

2
Timur Efimov On

Your question lacks details. But I will try to guess.

'ThreadPoolExecutor' manages pool of threads. When you submit task, it will use free thread to run task.

So, if you are immediately submit 20 tasks, only 4 will be processed concurrently (in your example max threads = 4*). And when one of the task will be finished, next will be processed.

And now look at your code - only one executor.execute(new Runnable(). So, only one task submitted.

May be you want smth like code below:


for(looping arraylist){
    executor.execute(new Runnable() {
                        @Override
                        public void run() {
                           operations related to calling proc...
                     }
    });
}

*4 threads until queue not full (100 waiting tasks in your code). After 104 submitted, new threads will be used (up to 10 at your code). Thanks 'Slaw', see his comment.