Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9098873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:28:12+00:00 2026-06-17T00:28:12+00:00

I want to set timeouts for threads which are executed within a thread pool.

  • 0

I want to set timeouts for threads which are executed within a thread pool. At the moment I have following code:

ExecutorService executor = Executors.newFixedThreadPool(8);
for(List<String> l: partition) {            
    Runnable worker = new WorkerThread(l);
    executor.execute(worker);
}       

executor.shutdown();
while (!executor.isTerminated()) {

}

The code just splits a big list of objects into sublists and process these sublist within single threads. But this is not the point.

I want to give each single thread in the thread pool a timeout. For only one thread in the pool I found following solution:

Future<?> future = null;

for (List<String> l : partition) {
    Runnable worker = new WorkerThread(l);
    future = executor.submit(worker);
}

try {
    System.out.println("Started..");
    System.out.println(future.get(3, TimeUnit.SECONDS));
    System.out.println("Finished!");
} catch (TimeoutException e) {
    System.out.println("Terminated!");
}

But this would not work for more than one thread. Maybe I have to put each thread in a List<Future> list and iterate over this list and set a timeout for each future object?

Any suggestions?

EDIT AFTER USING CountDownLatch:

CountDownLatch doneSignal = new CountDownLatch(partition.size());
List<Future<?>> tasks = new ArrayList<Future<?>>();
ExecutorService executor = Executors.newFixedThreadPool(8);
for (List<String> l : partition) {
    Runnable worker = new WorkerThread(l);
    tasks.add(executor.submit(doneSignal, worker));
}

doneSignal.await(1, TimeUnit.SECONDS);
if (doneSignal.getCount() > 0) {
    for (Future<?> fut : tasks) {
    if (!fut.isDone()) {
        System.out.println("Task " + fut + " has not finshed!");
        //fut.cancel(true) Maybe we can interrupt a thread this way?!
    }
    }
}

Works good so far.

So next question is how to interrupt a thread which is timed out? I try fut.cancel(true) and add following construct in some critical loops in the worker thread:

if(Thread.interrupted()) {
    System.out.println("!!Thread -> " + Thread.currentThread().getName() + " INTERRUPTED!!");
        return;
}

So the worker thread is “killed” after the timeout. Is this a good solution?

Furthermore: Is it possible to get the name of the thread which timed out over the Future interface? At the moment I have to print out the name in the if condition of the Thread.interrupted() construct.

Thanks for help!

Regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T00:28:13+00:00Added an answer on June 17, 2026 at 12:28 am

    Have you seen this? ExecutorService.invokeAll

    It should be exactly what you want: Invoke a bundle of workers and have them timeout if taking too long.

    EDIT after comment – (new idea):
    You can use a CountDownLatch to wait for the tasks to finish AND timeout via await(long timeout, TimeUnit unit)!
    You can then even do a shutdownNow and see which tasks have taken too long …

    EDIT 2:

    To make it clearer:

    1. Have a CountDownLatch be count down by each Worker, when finished.
    2. In the main execution thread await with timeout on said latch.
    3. When that call returns, you can check the Latches’s count to see if there has been the timeout hit (if it is >0).
    4. a) count = 0, all tasks finished in time.
      b) if not, loop the Futures and check their isDone. You don’t have to call shutdown on the ExecutorService.
    5. Call shutdown if you do not need the Executor any longer.

    Note: Workers can finish in the meantime between the timeout and calling their Future’s isDone().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hy!! I have a thread class and want to set a timeout inside after
My question is : I have an EditText and I want set a margin
i have a problem, i want set text of a UILabel or UItextView or
i have a multiple marker Google map, it works fine but i want set
Maybe i cant do what i want? I want to have 1 thread do
I have two functions that I want to run on different threads (because they're
If I have two threads (Linux, NPTL), and I have one thread that is
I have the following code that I'd like to test: public class DirectoryProcessor {
I have the following code on WCF Service and I am using basicHttpBinding public
Is there a way to Interupt a sleeping thread? If I have code similar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.