-- if I create scheduler via `Schedulers.from(Exec...
# rx
u
-- if I create scheduler via
Schedulers.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?
a
I highly recommend to read javadocs carefully and then dive into sources when needed, that way you don't have to trust someone-else's answer blindly and over time you'll get used to finding such answers to this on your own TL;TR: yes, it shuts down automatically on GC Long answer: - I didn't know either, normally I'd expect it to be GCed, otherwise Thread will keep running forever (as long as process is alive) - k, wat do - Start with
Executors.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
Copy code
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 implementation
u
well, I had a hunch it would be needed but then other Schedulers like io, computation are executors too, and you never shutdown those explicitly, but now that I think about it they are kept process wide so it doesnt matter, sorry, thanks for help