ursus
11/13/2018, 7:17 PMSchedulers.from(Executors.newSingleThreadExecutor { Thread(it, "SipCallManagerThread") })
, do I need to shutdown it explicitly like a native resource or is leaving it to be GCed okay?artem_zin
11/13/2018, 9:41 PMExecutors.newSingleThreadExecutor
javadoc
- welp, javadoc doesn't say anything about GC or automatic shutdown
- Open sources of Executors.newSingleThreadExecutor
- Notice that it creates new FinalizableDelegatedExecutorService
which kinda suggests finalization
- Open sources FinalizableDelegatedExecutorService
- Observe protected void finalize() { super.shutdown(); }
- Answer — it is garbage collected
One (probably @jw lol, that's a long debate of ours) however could (reasonably) recommend to shut it down yourself if you have a clear point in your program lifecycle for that.
The reason for such recommendation (understandably) is that service.shutdown()
can take a while, and potentially block an important GC thread thus potentially increasing the GC pause (either global or thread based)
However, javadoc of Object.finalize()
mentions use cases like this
For example, the finalize method
* for an object that represents an input/output connection might perform
* explicit I/O transactions to break the connection before the object is
* permanently discarded.
finalize
javadoc also says that there is no guarantee on which thread would call this method, so in theory it could be ok to do blocking I/O, but that's up to JVM/GC implementationursus
11/13/2018, 11:37 PM