Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
How to shutdown the ThreadServiceExecutor where the number of threads is not initially known

I am in the process of writing a code where whenever a folder is encountered it is supposed to start a new thread. The code looks like, p

ublic void diff(File x,File y){
    ThreadPoolExecutor executor=new ThreadPoolExecutor(10,30,2000,unit,BlockingQueue<Runnable> queue)
    if(x.isDirecotry && y.isDirectory){
      Runnable thread=new DThread(x,y);
        Future<?> result=executor.submit(thread);
        if(result.isDone()){
            LOGGER.debug(thread.toString()+"has completed");
        }

I am using ThreadPoolExecutor for this purpose. If I shutdown the ThreadPoolExecutor then it will not take up any new Threads. But there is a possibility of new threads starting after starting. If I do not shutdown the ThreadPoolExecutor then all the threads are executed but in the end the JVM is not terminating. Please help how can I shutdown the threadPool only when all the threads are executed so that the JVM gets terminated. Also suggest if there is better way of implemention of thread pool.I want to use thread pool so that I can use threads from the pool instead of creating new thread everytime.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The things that you are submitting should not be threads (subtypes of Thread). They should be simple tasks: implementations of Runnable or Callable.

The way to shutdown the executor is to call shutdown() or shutdownNow() on it. The javadoc summary says:

void shutdown() - Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.

List<Runnable> shutdownNow() - Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.

The latter attempts to stop the tasks by interrupting them. However, if the task code does not check for interrupts, it will run to completion. (It also stops accepting new tasks.)


It sounds like shutdown() does what you want to do.


The problem lies in the fact where should I place the shutdown method.

You call it when you want to start shutting down.

Because there is a possibility that new threads may be coming after shutdown is being called.

Stop calling them threads. They are tasks.

Any tasks that are submitted after shutdown() has been called are rejected.

My question is how can I check whether all the tasks have finished executing and only then call shutdown. If I call with the initiation of each task then it will not allow new tasks later on.

The correct time to call shutdown() is when your application has finished submitting tasks to the executor.

Maybe your conceptual problem is the scoping of the executor service. Your example code seems to show that each call to diff is creating a new service object. That means that you would have lots of independent thread pools ... and no reuse of threads. What you should really do is to create a "global" thread pool and have multiple calls to diff submit tasks to the same pool.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!