How to get to FutureTask execution state?

前端 未结 4 1012
情话喂你
情话喂你 2021-01-05 20:50

I have a singleThreadExecutor in order to execute the tasks I submit to it in serial order i.e. one task after another, no parallel execution.

I have runnable which

4条回答
  •  囚心锁ツ
    2021-01-05 21:45

    If you wanted to be really thorough, FutureTask keeps track of states READY, RUNNING, RAN, and CANCELLED internally. You could create a copy of this class and add an accessor for the state. Then override AbstractExecutorService.newTaskFor(Runnable) to wrap it using your CustomFutureTask (the inner class is private, so just subclassing won't work).

    The default implementation of newTaskFor(Runnable) is really simple:

    protected  RunnableFuture newTaskFor(Runnable runnable, T value) {
        return new FutureTask(runnable, value);
    }
    

    so it wouldn't be a big deal to override it.

提交回复
热议问题