agasthya_tadakamalla
agasthya_tadakamalla

Reputation: 114

Closing Threads in Executor Service without shutting it down

My code for executor service is shown below. I am executing multiple threads. But for some reason the program is not exiting as there are some threads in waiting state according to the logs. If I use Thread.start(), then I don't have that problem. I am facing the problem only when I am using ExecutorService.Can anyone tell me how to close all the threads which executed successfully without shutting down ExecutorService.

class ExecutorExample{
    public static void main(){
        File folder = new File("/home/agasthyt/agasthyt");
        File[] listOfFiles = folder.listFiles();
        for (int i = 0; i < listOfFiles.length; i++) {
            Thread1 thread1=new Thread1(listOfFiles[i]);
            es.execute(thread1);
    }
  }
}

class Thread1 extends Thread{public void run(){......}}

Upvotes: 0

Views: 1651

Answers (2)

Mattia
Mattia

Reputation: 1

You can use Executors class that provides newFixedThreadPool method, so in this way you can select the max number of threads that you want to use. Exceutors will use the same number of threads and will reuse the space if necessary. I attached the documentation

enter code hereint maxNumberOfThread=10;
ExecutorService pool=Executors.newFixedThreadPool(maxNumberOfThread);
... 
pool.execute(new Thread1(listOfFiles[i]));

Documentation Executors, ExecutorService

Upvotes: 0

Solomon Slow
Solomon Slow

Reputation: 27190

Can anyone show me how to close all the threads which executed successfully without shutting down ExecutorService.

That is not what ExecutorService is for. When you create an ExecutorService, you give it tasks to perform, and you let it manage the threads that it uses to execute your task. You aren't supposed to know or care about the threads that it uses.

When you don't want it anymore (e.g., when you want to terminate the program), then you shut it down.


P.S., Your Thread1 class extends Thread. That's a bad habit that leads you into a less-useful way of thinking about threads. The right way to think about threads and executors is that a thread or an executor is just this thing that executes some piece or pieces of code that you give it. Nothing more.

I've seen a lot of questions that say things like, "I need the X thread to do Y, but...", and what's leading them into trouble is that the developer has given the X thread too many responsibilities.

In a well-designed program, it should not matter which thread does Y. Threads should be anonymous and interchangeable.


Update:

Can you suggest any method to close the threads without shutting down executor service.

No, I can't. If the threads were created by an ExecutorService, then it would be wrong for your program to kill those threads behind its back.

Q: What class are you using that implements ExecutorService? Is it a ThreadPoolExecutor perhaps? Perhaps you could solve your problem by creating a ThreadFactory that makes daemon threads, and then construct your ThreadPoolExecutor using that factory. A daemon thread will not stop the program from exiting.

Upvotes: 1

Related Questions