When is the garbage collected in the ExecutorService running?

I have a runnable object A that exchanges heartbeat signals with a server when creating an instance. I am sending n such objects to an executing service with a fixed thread pool size of n. When the start method encounters an exception, it will return. For this case, all my flows are met with exception and return, but the created object remains alive and continues to exchange heart rate signals. How to mark such garbage collection objects so that they stop the exchange of heart rate signals?

class A implements Runnable { public void run(){ try{ \\throws error } catch(Exception e){ \\returns } } public static void main(){ ExecutorService executor = Executors.newFixedThreadPool(n) for(i = 1 to n){ A a = new A() executor.submit(a) } } } 

Should I put a wait call at the end of my master record and make a refund?

Edit:
Putting the question in a different way, one of the ways to terminate the executing service after all the return flows are called by shutdown () after the for loop and call awaitTermination with Integer.MAX for long seconds, which is approximately 70 years (which is a temporary limitation, I’m reluctant impose). Is there any other alternative?

+5
source share
3 answers

one way to terminate the execution service after all the return flows have called shutdown () after the for loop and call waitTermination with Integer.MAX for long seconds, which is approximately 70 years

as the document says the awaitTermination method blocks use:

  • all tasks completed after completion request
  • or there will be a timeout
  • or the current thread is interrupted, depending on what happens first


So, he will enter the game as soon as one of the three events appears, instead of waiting for 70 years.

+1
source

calling shutdown () in the pool means that the pool will no longer accept a new task for execution, but the current ones will work without interruption.

calling awaitTermination (timeout) holds the calling thread until the pool ends, but if the timeout is reached, the current thread will issue execption, but it will not affect the tasks in the pool.

If your runnable throws an unused exception when starting a thread pool, then this runnable is no longer in a run state - the thread pool usually does not refer to such an object.

If you use FixedThreadPool, then this pool will create as many threads as you like, and will not stop them until you call shutdown () in this pool.

Unless you have a reference to the runnable object that threw the exception, it behaves like a regular unpublished object that will be garbage collected.

if you call shutdown () and then wait for Termination () in the thread pool, and your program does not stop anyway, this means that not all instances of your runnable throw an exception, and some still work, blocking the pool from full completion .

In java, you cannot kill or stop a thread from starting in the same way (you can only kill an entire JVM using, for example, System.exit(0) , but not just select a thread), if you need such functionality, you need to program the runnable body in a way that allows you to somehow exchange with it, i.e. using some variable " volatile boolean " and that it will respond to a change in the value of this variable - this means that you need to add "if it checks" the value of this variable in the body of the run () method, which will return when it is.

0
source

tasks themselves can be collected to collect garbage as soon as their execution is completed. If and when they are actually collected, it depends on the garbage collector.

Code example:

 public class Main implements Runnable { @Override protected void finalize() throws Throwable { super.finalize(); System.out.println("finalize"); } @Override public void run() { try { throw new Exception("Error"); } catch (Exception e) { //returns } } public static void main(String args[]) { int n = 8; ExecutorService executor = Executors.newFixedThreadPool(n); for (int i = 0 ; i < n; ++i) { Main a = new Main(); executor.submit(a); } System.gc(); System.out.println("end"); } } 
0
source

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


All Articles