0
ExecutorService executor = ****;
List<List<Object>> objectGroup = Lists.partition(objectList, 5);
for (List<Object> eachGroup : objectGroup ) {
            CountDownLatch latch = new CountDownLatch(eachGroup.size());
            for (Object obj: eachGroup) {
                executor.submit(() -> {
                    doTask(obj);
                    latch.countDown();
                });
            }

            try {
                if (!latch.await(15, TimeUnit.MINUTES)) {
                    // todo close all thread in executor 
                }
            }
            catch (InterruptedException e) {
                System.out.println();
            }

        }

**

I want to close all thread in executor after 15 minutes but I do not know how to close.

Someone said to use executor.shutDownNow() but it will close the executor (I can not submit thread anymore), I don't want that to happen

**

Alez
  • 1,913
  • 3
  • 18
  • 22
musan
  • 51
  • 6
  • You want to close the executor service but don’t want to call the shutdown method??? Why not? – Abhijit Sarkar Nov 02 '22 at 09:58
  • "Someone said to use executor.shutDownNow() but it will close the executor (I can not submit thread anymore), I don't want that to happen"? Why you don't want that to happen? What would be issue? – aatwork Nov 02 '22 at 09:59
  • I think this may be relevant: [shutdown and awaitTermination which first call have any difference](https://stackoverflow.com/questions/18425026/shutdown-and-awaittermination-which-first-call-have-any-difference) Also refer to the [documentation](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/concurrent/ExecutorService.html). – Abra Nov 02 '22 at 10:01
  • I want to stop/interrupt the thread work, but not close executor – musan Nov 02 '22 at 10:01

1 Answers1

0

There's only one way to stop the Thread in java: ask it to do so.

new Runnable()  {
   // This status should be checked repeatedly within a process
   if(Thread.interrupted()) {
       // Executore has probably ask to stop
   }
}

Further, you have an ExecutorService instance. To ask all threads to stop, you should call shutDownNow() method. This method will set interrupted flag to all threads in the ExecutorService.

P.S. Looks like this post gives the details.

Abra
  • 19,142
  • 7
  • 29
  • 41
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35