The number of active threads continues to increase

I use the executorService function to exclude only one task at a time using this code `

executorService=Executors.newSingleThreadExecutor();

and I use Thread.activeCount()to get the number of active threads, but every time I send a task to the executing service, the number of active threads increases by one, how is this possible? I thought that it newSingleThreadExecutor()allows you to perform only one task at a time, why does the number of threads continue to increase ?, I mean, should the number of threads increase by only one and no more?

Please note that I also use the future to cancel the execution of all runnable before even sending a new task, and it works fine, all runnables are interrupted, but the number of active threads continues to increase.

Edit: this is the code that I call whenever I click the button (Worker is just a class that implements runnable)

private void handle() {
    executorService=Executors.newSingleThreadExecutor();
    Worker worker=new Worker();
    future=executorService.submit(new Worker());
}
+4
source share
1 answer

Each time you call him, he creates a new Contractor. Each individual streaming artist has its own stream. If you want to complete several tasks for this artist, you call newSingleThreadedExecutor once and send all the tasks to this artist. You do not call newSingleThreadedExecutor several times

+2

Source: https://habr.com/ru/post/1650528/


All Articles