Results 1 to 4 of 4

Thread: Thread pool - setting time out for a thread

  1. #1

    Default Thread pool - setting time out for a thread

    hi,

    i m new to spring. i m trying to use thread pool for a project. my requirement is to timout the thread if the processing goes beyond x min.

    i checked many properties in taskexecutor, but none is able to achieve this. may be i m missing something key there...

    any suggestions?


    arav

  2. #2
    Join Date
    Oct 2009
    Posts
    7

    Default

    Quote Originally Posted by aravind_6776 View Post
    hi,

    i m new to spring. i m trying to use thread pool for a project. my requirement is to timout the thread if the processing goes beyond x min.

    i checked many properties in taskexecutor, but none is able to achieve this. may be i m missing something key there...

    any suggestions?


    arav
    using ThreadPoolExecutor#awaitTermination

    Code:
    if(!executor.awaitTermination(xx, TimeUnit.Min)) {
        throw new XXTimeoutException();
    }

    http://java.sun.com/javase/6/docs/ap...lExecutor.html

  3. #3

    Default Thread pool - setting time out for a thread

    hi,

    thanks for your help. i tried this but it seems, it blocks all the threads to wait till the timeout period even if it successfully complete the processing with in timeout period set.

    i need to apply the timeout only to those threads which exceed the limit. I m trying to make sure if any thread is hung due to any reason, it must be killed or timeout after x mins.

    is it possible to achieve this?

    Aravind

  4. #4
    Join Date
    Oct 2009
    Posts
    7

    Default

    Quote Originally Posted by aravind_6776 View Post
    hi,

    thanks for your help. i tried this but it seems, it blocks all the threads to wait till the timeout period even if it successfully complete the processing with in timeout period set.

    i need to apply the timeout only to those threads which exceed the limit. I m trying to make sure if any thread is hung due to any reason, it must be killed or timeout after x mins.

    is it possible to achieve this?

    Aravind
    en, post the code for you

    Code:
    // create an timeout thread
    
    public class TimeoutRunnable implements Runnable {
    
    private long limitTime;
    
    private Runnable runnable;
    
    privaate ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 601, TimeUnit.MINITUE, new LinkedBlockQueue());
    
    public TimeoutRunnable(long limitTime, Runnable runnable) {
       this.limitTime = limitTime;
       this.runnable = runnable;
    }
    
    public void run() {
      executor.excutor(runable);
      if(executor.awaitTerminate(....)) {
        throw new TimeoutException();
       }
    
      executor.shutDown();
    
    }
    
    }

    put your runnable implements into this class, DO NOT FORGET INVOKE SHUTDOWN

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •