0

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.

Debdipta Halder
  • 497
  • 1
  • 5
  • 19

1 Answers1

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • It does what I want. I am not using a Thread. I am using a Runnable implementation. The problem lies in the fact where should I place the shutdown method. Because there is a possibility that new threads may be coming after shutdown is being called. My question is how can I check whether all the threads have finished executing and only then call shutdown. If I call with the initiation of each thread then it will not allow new threads later on. – Debdipta Halder Apr 28 '17 at 14:40