title: Each thread should run for 10 minutes and then interrupt the thread if the time is over
body: <p>I am trying to make an <code>ExecutorService</code> implementation that can be provided with a timeout or interrupt for each thread.</p>
  
  <p>In my below example, suppose I am spawning <code>2 threads</code> (in actual scenario, this number will be high), then I need to make sure <code>each thread</code> should be running for <code>10 minutes</code>.
  That means, <code>Thread1 will run for 10 minutes</code> and <code>Thread2 will run for 10 minutes as well</code>. If 10 minutes is over then I need to interrup the thread or timeout.</p>
  
  <p>Below is the code I have so far and I am not able to understand how should I add this <code>interrupt or timeout</code> functionality here in such a clean way so that if I am making this <code>no of threads</code> parameter configurable in my code then it should work properly there as well.</p>
  
  <pre><code>public static void main(String[] args) {
  
      final int noOfThreads = 2;
      final long exp_time_millis = 600000; //10 minutes
  
      //create thread pool with given size 
      ExecutorService service = Executors.newFixedThreadPool(noOfThreads);
  
  
      for (int i = 0, i&lt; noOfThreads; i++) {
          service.submit(new ThreadTask());
      }
  }
  
  
  class ThreadTask implements Runnable {
  
      @Override
      public void run() {
  
          while(true) {
              System.out.println("Thread running...");
              try {
  
          /* make a select sql to the database 
           * and measure how much time it is taking in 
           * returning the response
           */
  
              } catch (InterruptedException e) {
  
              }
          }
      }
  }
  </code></pre>
  
  <p>Any suggestions will be of great help.</p>
  
  <p>I have already seen few articles on the SO but I was not able to find anything which matches my scenario and I can implement that easily.</p>
  
  <p><strong>Updated Code:-</strong></p>
  
  <p>I am trying the below code but it gives me error on the catch block in the run method. Not sure if I am doing anything wrong. Can anyone help me?</p>
  
  <pre><code>public class ThreadTimeout {
  
      public static void main(String[] args) {
  
          final int noOfThreads = 2;
  
          //create thread pool with given size 
          ExecutorService service = Executors.newFixedThreadPool(noOfThreads);
  
          ScheduledExecutorService scheduleService = Executors.newScheduledThreadPool(noOfThreads);
          for (int i = 0; i&lt; noOfThreads; i++) {
              final Future future = service.submit(new ThreadTask());
              scheduleService.schedule(new Runnable(){
                  public void run(){
                      future.cancel(true);
                  }
              }, 10, TimeUnit.MINUTES);
          }
      }
  }
  
  class ThreadTask implements Runnable {
  
      @Override
      public void run() {
  
             //make a database connection
  
          while (true) {
              System.out.println("Thread running...");
              try {
                  /*
                   * make a select sql to the database and measure
                   * how much time it is taking in returning the
                   * response
                   */
              } catch (InterruptedException e) {
  
              }
          }
      }
  }
  </code></pre>