Announcement Announcement Module
Collapse

Spring Modules forum decommissioned in favor of Spring Extensions

As the Spring Modules project has been replaced by the Spring Extensions (http://www.springsource.org/extensions) project, this forum has been decommissioned in favour of Spring Extensions one at:
http://forum.springsource.org/forumdisplay.php?f=44

Please see the Spring Extensions home page for a complete list of current projects in Java, .NET and ActionScript. You can also propose one if you want.

Cheers,
Costin Leau
SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
http://twitter.com/costinl
See more
See less
Thread pool - setting time out for a thread Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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

    Comment


    • #3
      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

      Comment


      • #4
        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

        Comment

        Working...
        X