I’ve been toying with a very similar problem recently in my Praxis project. Praxis has a live compiler which allows the user to run fragments of live code in the video pipeline. Useful, and fun to play with, but I’d like to have a ‘sandboxed’ mode that doesn’t bring the video pipeline juddering to a halt if you pass in some dodgy code.
SO … best I’ve thought of so far would be to use submit() instead of execute() on the ExecutorService, and have a second monitoring thread that calls get(timeout…) on the returned Future. If it times out, you can then call cancel(true) on the Future to interrupt the task, but it does depend on the code handling interrupts gracefully. If you’ve got control of the tasks, that shouldn’t be a problem. If it’s code you can’t control (like me), handling things like hard loops and clearing up after a misbehaving Runnable, seem harder to implement. Thread.stop() anyone??? ;D
Hope that maybe helps you anyway.
hmm … but hidden in full view within this “advice”, which yes doesn’t touch the question … the idea of making the Manager a singleton I’d recommend, particularly with a getDefault() like factory method. Allows you to easily inject different implementations for different purposes / testing.
Best wishes, Neil