I need to block the current thread until I call one of two following methods (which I created).
onJobComplete()
onJobError(Throwable t)
These methods will be called from a different thread.
Is this possible with a CountDownLatch(1)
? (which I would decrement when either of those two methods are called). Seems I can only use CountDownLatch
with a new thread.
If not, how can I accomplish this?
background: https://github.com/ReactiveX/RxJava/issues/5094 (I need to make the below synchronous onRun()
method from a 3rd party library asynchronous)
/**
* The actual method that should to the work.
* It should finish w/o any exception. If it throws any exception,
* {@link #shouldReRunOnThrowable(Throwable, int, int)} will be
* automatically called by this library, either to dismiss the job or re-run it.
*
* @throws Throwable Can throw and exception which will mark job run as failed
*/
abstract public void onRun() throws Throwable;
More info: Because of library limitations, I cannot control when onRun()
starts. The library needs this method to be synchronous, because its completion automatically signals to the library that the "job" successfully completed. I want to "pause" onRun()
and prevent it from returning, kick off my own asynchronous thread(s), and "resume" onRun()
(allow onRun()
to return) once my asynchronous thread completes.