“任务”可以处于挂起、通过或失败状态。转换到“已通过”状态的“待处理”任务将与进行相同转换的“失败”任务区别对待。
That seems a rather odd collection of states. I'd expect a task to take some time to execute, so transition from pending to executing then either pass or fail, and tasks which don't execute before their time runs out to expire. If there is also a transition from failed to failed-and-expired then that might add another state.
Draw a state machine to find the states.
Firstly, do you need to model the states structurally? Would a pending/expired flag, a scheduled time and a result do (with failure and success as the two sub-types of result)? What do clients of the task need from it?
其次,您是在与任务还是调度程序交互?给调度器一个任务描述并返回一个未来,这可以查询任务的结果,这并不少见。但任务本身并没有暴露,只有是否完成和结果。如果您需要进度,您可能需要一个可以通过任务 ID 查询以获取进度的调度程序,而不是您持有引用的任务对象 - 拥有一个同时更改状态的任务对象很难获得一致的集合状态数据,直到它达到最终状态。如果“通过”状态没有失败信息,那么查询“你失败了吗”,然后是“获取失败状态”很容易导致竞争,除非你将锁定外部化(ewww),