I have a thread which is looping a queue, reading tasks and delegating them to a CachedThreadPool. This queue is continuously being filled with client-submitted tasks.
I want the reader thread to terminate if the client programs also terminates. At first I thought that setting the reader thread as a daemon would solve the problem. However, there are two problems with this:
- The reader thread needs to read all the events still left in the queue before it exits.
- The reader thread has a CachedThreadPool. Since the threads in it are not daemon, the program will no exit. If I set all the threads in the pool as daemon, somehow they do not process all the work (probably because the reader will terminate before processing all the queue).
I also tried to solve this with a shutdown hook, however the hook is never called at the right time (maybe because I'm running the client from a build tools? (Scala's SBT)).
Any hints on getting around this?