Animesh Sahu
08/06/2019, 4:29 AMkralli
08/06/2019, 7:05 AMPromise
is causing the error. More specifically the following part:
val r = result
when {
r !== null -> return r.done(onFulfilled)
else -> handlers.add { promise: PromiseInterface -> promise.done(onFulfilled) }
}
What happens inside your code is, that the promise is done even before .done { }
has been called. But because you return null
as a result of the task, the implementation of done
is assuming that the promise isn’t done yet and registers the handler. You need to maintain the state of the promise separately and thread-safe.join
is never called, because there are still non-daemon threads running like the timer in init
of Pool
and the Worker
itself.currentlyBusy
and lastEmptyStack
in Worker
are not thread-safe. Also the logic in run
does not account for a potential change in size of the queue between isEmpty()
and dequeue()
. This could cause the thread to be marked as busy even though it’s waiting for a task.