3

I'm looking for a simple object that will hold my work threads and I need it to not limit the number of threads, and not keep them alive longer than needed. But I do need it to have a method similar to an ExecutorService.shutdown(); (Waiting for all the active threads to finish but not accepting any new ones)

so maybe a threadpool isn't what I need, so I would love a push in the right direction. (as they are meant to keep the threads alive)

Further clarification of intent:

each thread is an upload of a file, and I have another process that modifies files, but it waits for the file to not have any uploads. by joining each of the threads. So when they are kept alive it locks that process. (each thread adds himself to a list for a specific file on creation, so I only join() threads that upload a specific file)

2 Answers 2

6

One way to do what you awant is to use a Callable with a Future that returns the File object of a completed upload. Then pass the Future into another Callable that checks Future.isDone() and spins until it returns true and then do whatever you need to do to the file. Your use case is not unique and fits very neatly into the java.util.concurrent package capabilities.

One interesting class is ExecutorCompletionService class which does exactly what you want with waiting for results then proceeding with an additional calculation.

A CompletionService that uses a supplied Executor to execute tasks. This class arranges that submitted tasks are, upon completion, placed on a queue accessible using take. The class is lightweight enough to be suitable for transient use when processing groups of tasks.

Usage Examples: Suppose you have a set of solvers for a certain problem, each returning a value of some type Result, and would like to run them concurrently, processing the results of each of them that return a non-null value, in some method use(Result r). You could write this as:

   void solve(Executor e, Collection<Callable<Result>> solvers)
              throws InterruptedException, ExecutionException 
   {
       CompletionService<Result> ecs = new ExecutorCompletionService<Result>(e);
       for (Callable<Result> s : solvers) { ecs.submit(s); }
       int n = solvers.size();
       for (int i = 0; i < n; ++i) 
       {
           Result r = ecs.take().get();
           if (r != null) { use(r); }
       }
   }

You don't want an unbounded ExecutorService

You almost never want to allow unbounded thread pools, as they actually can limit the performance of your application if the number of threads gets out of hand.

You domain is limited by disk or network I/O or both, so a small thread pool would be sufficient. You are not going to want to try and read from hundreds or thousands of incoming connections with a thread per connection.

Part of your solution, if you are receiving more than a handful of concurrent uploads is to investigate the java.nio package and read about non-blocking I/O as well.

Sign up to request clarification or add additional context in comments.

Comments

0

Is there a reason that you don't want to reuse threads? Seems to me that the simplest thing would be to use ExecutorService anyway and let it reuse threads.

2 Comments

each thread is an upload of a file, and I have another process that modifies files,but it waits for the file to not have any uploads. by joining each of the threads. so when they are kept alive it locks that process. (each thread adds himself to a list for a specific file on creation, so I only join() threads that upload a specific file)
you're conflating two concepts: the thread and the task. The task is "upload this file", the thread is a re-usable resource for accomplishing the task. The task (Runnable for the upload) can signal the "other process" when it is done without calling join() on a thread. How exactly does the "other process" receive these signals?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.