"simple" thread pool in java

I am looking for a simple object that will contain my worker threads, and I need it to not limit the number of threads and not support them longer than necessary. But I need it to have a method similar to ExecutorService.shutdown(); (Waiting for all active threads to complete, but not accepting new ones)

so maybe threadpool is not what i need, so i would like to push in the right direction. (since they are designed to maintain a live stream)

Further clarification of intent:

each thread is a file upload, and I have another process that modifies the files, but it expects the file to not have any downloads. by combining each of the threads. Therefore, when they remain alive, it blocks this process. (each thread adds itself to the list for a specific file when created, so I only join () the threads that load a specific file)

+3
source share
2 answers

, , - Callable Future, File . Future Callable, Future.isDone() , true, , . java.util.concurrent .

- ExecutorCompletionService , , , , .

A CompletionService, . , , , take. , , .

. , , , , ( r). :

   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); }
       }
   }

ExecutorService

, , - .

- , . .

, , , - java.nio package -.

+6

, ? , ExecutorService .

0

All Articles