I want to implement a service, where some work happens periodically in its own thread.
However outside client should be able to pause the thread for a while, and then resume it
I have been able to implement the same using ScheduledExcutorService and semaphore. however i am not satisfied with the implementation. As you can see in the code below, it has become necessar for the client to pass a semaphore instance to the running task.
i am in doing smthing dont distubr 2018-06-25T21:51:49.623
i am in doing smthing dont distubr 2018-06-25T21:51:54.626
i am in doing smthing dont distubr 2018-06-25T21:51:59.628
i am in doing smthing dont distubr 2018-06-25T21:52:04.630
----clear waiting time...i am able to pause and resume-------
i am in doing smthing dont distubr 2018-06-25T21:52:29.558
i am in doing smthing dont distubr 2018-06-25T21:52:34.558
i am in doing smthing dont distubr 2018-06-25T21:52:39.559
Well, ingenious with that Semaphore. Disadvantage might be the creation of a new MyOwnPausable after each fixed delay.
You could implement MyOwnPausable as, say, a Runnable, that could sleep a given time after doing its job, and that you can interrupt any time you like. You only require one Runnable and one Thread in the ThreadPool, in that case. If that makes it any better or simpler, I'm not sure.
There are three kinds of actuaries: those who can count, and those who can't.
What if instead of the client passing in a semaphore and acquiring/releasing that, the semaphore itself was internal and and MyOwnPausable provided its own methods for acquiring/releasing the semaphore?
Then you wouldn't have to pass anything to the constructor, though you would have to retain a reference to the MyOwnPausable instance instead.
Friends help you move. Good friends help you move bodies. This tiny ad will help: