I'm using ScheduledThreadPoolExecutor.schedule(Runnable,int,TimeUnit) to schedule some objects of a class that implements Runnable.
At some point in time, my application is then shutting down, and I use ScheduledThreadPoolExecutor.shutdownNow(). According to the documentation it returns a list of ScheduledFuture's.
What I really want to do is get a hold of the object that I originally scheduled, and get a bit of data from it which I will then output saying that it was unable to execute. This would then, later, be used by the application to attempt to execute it when the application then starts back up.
The usual way to get info about objects submitted to an executor is to maintain a pointer to the original objects (be they extensions to Callable, Runnable, etc). After you call shutdownNow(), and take into account the Runnable's returned by that which were awaiting execution, you can use that to prune your original list of objects and just interrogate the ones that were actually run.
If you just want to present the information to the user, the simplest approach might be to implement a meaningful toString()
method for the Runnable
s you'r scheduling. Then you can simply iterate the list the Executor
gives you and log what you get.
But the sad truth is that your original objects get wrapped by the Executor
, though. Then you would need to keep a list of what you pass to the Executor
manually and let the Runnable
s remove themselves from this list when they get executed. Obviously, you would need to use a thread-safe list for this purpose.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With