java - ExecutorService awaitTermination shutdown signal in the Runnable instance -
i've few questions around executorservice
, shutdown process. use case: use executorservice
spawn fixed number of threads run method this:
while (true) { try { this.currentthreadrunning = true; processmessage(); } catch (throwable e) { // keeping thread alive despite exceptions. } }
these threads run infinitely, polling messages.
what trying do? polling sqs queue messages , processing them.
obviously, in case, executorservice's
shutdown method not work. when shutdownnow()
called, threads shutdown unceremoniously. hate it!
is there way invoke awaittermination
, verify, in runnable
instance(in block?), if shutdown has been initiated , trigger same current thread?
update: i've refactored code perform polling , spawning threads process them. thus, runnable instance's run method need not endless loop. , awaitermination
lead definite closure of threads. , sure, i've triggered shutdownnow
after awaittermination
.
i think doing conceptually wrong.
awaittermination
meant wait threads finish naturally , stop executor. when submitting runnable
, shouldn't have idea of context of it's execution, so, coupling runnable executor not idea imho.
maybe should future
class , move runnable
implementation there. forced implement cancel(boolean)
method might find useful.
what use case? maybe if explain it, community can point out better suited implementation.
Comments
Post a Comment