The problem is that the scheduler maintains a live stream after you cancel the audio task.
If there is a live stream without a demon, the JVM stays alive.
The reason he supports this thread is because you told him to do this on this line:
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
Check out the newScheduledThreadPool(int corePoolSize) documentation:
corePoolSize - the number of threads to store in the pool, even if they do not work.
So, you have two possible ways: the JVM may terminate:
Pass 0 to newScheduledThreadPool instead of 1. The scheduler will not support the live thread, and the JVM will newScheduledThreadPool .
Complete the scheduler. You still have to do this to free up your resources. Therefore, change run in anonymous Runnable to:
public void run() { beeperHandle.cancel(true); scheduler.shutdown(); }
(Actually, you donβt need cancel - shutdown takes effect as soon as the next βbeepβ is completed.)
Realskeptic
source share