0

In my application we have used Thread pool, we have specified a timeout to the thread pool but it seems that the timeout is not called, below is the code :

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class ThreadPoolDemo extends Thread{

public void run(){
    System.out.println("Starting---" + new Timestamp((new Date()).getTime())  + "--" + Thread.currentThread().getName());
    try {
        Thread.sleep(30000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("Finishing---" + new Timestamp((new Date()).getTime())  + "--" +Thread.currentThread().getName());
}

public static void main (String[] args){
    ArrayBlockingQueue<Runnable> threadQueue = new ArrayBlockingQueue<Runnable>(5);
    ThreadPoolExecutor thumbnailGeneratorThreadPool = new ThreadPoolExecutor(1, 3,
            5, TimeUnit.SECONDS, threadQueue);
    thumbnailGeneratorThreadPool.allowCoreThreadTimeOut(true);
    ArrayList fTasks = new ArrayList();
    for (int i = 0; i < 15; i++) {
        System.out.println("Submitting Thread : " + (i+1) + "- Current Queue size is : " + threadQueue.size());
        ThreadPoolDemo tpd = new ThreadPoolDemo();
        Future future = thumbnailGeneratorThreadPool.submit(tpd);
    }
    }
}

The out put of the code is :

Submitting Thread : 1- Current Queue size is : 0
Submitting Thread : 2- Current Queue size is : 0
Submitting Thread : 3- Current Queue size is : 1
Submitting Thread : 4- Current Queue size is : 2
Submitting Thread : 5- Current Queue size is : 3
Submitting Thread : 6- Current Queue size is : 4
Submitting Thread : 7- Current Queue size is : 5
Submitting Thread : 8- Current Queue size is : 5
Submitting Thread : 9- Current Queue size is : 5
Exception in thread "main" java.util.concurrent.RejectedExecutionException
    at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1774)
    at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:768)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:656)
    at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:78)
    at ThreadPoolDemo.main(ThreadPoolDemo.java:33)
Starting---2016-10-26 14:20:16.254--pool-1-thread-2
Starting---2016-10-26 14:20:16.254--pool-1-thread-3
Starting---2016-10-26 14:20:16.254--pool-1-thread-1
Finishing---2016-10-26 14:20:46.261--pool-1-thread-1
Finishing---2016-10-26 14:20:46.261--pool-1-thread-2
Finishing---2016-10-26 14:20:46.261--pool-1-thread-3
Starting---2016-10-26 14:20:46.261--pool-1-thread-2
Starting---2016-10-26 14:20:46.261--pool-1-thread-3
Starting---2016-10-26 14:20:46.261--pool-1-thread-1
Finishing---2016-10-26 14:21:16.265--pool-1-thread-1
Starting---2016-10-26 14:21:16.265--pool-1-thread-1
Finishing---2016-10-26 14:21:16.265--pool-1-thread-3
Starting---2016-10-26 14:21:16.265--pool-1-thread-3
Finishing---2016-10-26 14:21:16.265--pool-1-thread-2
Finishing---2016-10-26 14:21:46.277--pool-1-thread-1
Finishing---2016-10-26 14:21:46.277--pool-1-thread-3

Now in the ThreadPoolExecutor the keepAliveTime is set to 5 seconds.

However if we see the output the thread takes 30 seconds to complete. I'm not sure why the InterruptedException is not being called by the ThreadPoolExecutor on the thread.

I would like a mechanism to stop the threads if the thread is still active beyond the timeout specified.

Draken
  • 3,134
  • 13
  • 34
  • 54
Abhijit
  • 3
  • 1
  • 2
  • `allowCoreThreadTimeOut()` only allows threads to die *if there are no tasks* for them within your 5 seconds keep-alive time. The executor does not interrupt the threads nor disallow them from taking up tasks. You may want to look at the `shutdown()` and the `shutdownNow()` methods. – Ole V.V. Oct 26 '16 at 09:38
  • As an aside, your `ThreadPoolDemo` class needs not extend `Thread`, it’s enough to implement `Runnable`. – Ole V.V. Oct 26 '16 at 09:39
  • ThreadPoolDemo was just a fast implementation of the issue I was facing. I need a mechanism to interrupt a thread if the thread is active beyond a specific time – Abhijit Oct 26 '16 at 09:50

1 Answers1

0

As already indicated in the comments, you did not specify a time out. You only specified the keep-alive time of the ThreadPoolExecutor. The keep-alive does not terminate or interrupt running threads, but only releases idle threads of the Executor (see getKeepAliveTime).

If you want to set a time out for your tasks, you have to use the invokeAll or invokeAny methods instead of submit. See also

Community
  • 1
  • 1
Martin Nyolt
  • 4,463
  • 3
  • 28
  • 36