Search code examples
javajvmoptaplanner

OptaPlanner background thread not terminating


We are running OP (v8.11.1.Final) inside a standalone Java application (so we can evaluate it), but have noticed that once the solver has completed and returned the solution, the JVM will not terminate. It appears that there is a background thread still kicking around once we have returned from the main method that is keeping the JVM alive. The only way to get around this is to perform a System.exit() which does not seem that nice a solution.

Here are the list of threads still active. I guess some of these are JVM management threads, but something is still running causing this issue.

enter image description here

Any ideas? If you need more info let me know. Thanks in advance.


Solution

  • Call SolverManager.close() when you're done using it. In Quarkus and Spring Boot, this happens automatically.

    Or better yet, use it in an ARM block:

    try (SolverManager<...> solverManager : SolverManager.create(...)) {
        ...
    }
    

    The SolverManager is a wrapper around a ThreadPool. And just like a ThreadPool needs to be explicitly closed, so does the SolverManager (so it can close its internal ThreadPool).