2 回答

TA貢獻(xiàn)2041條經(jīng)驗(yàn) 獲得超4個(gè)贊
看一段偽代碼:
while (true) {
task = fetch task
If no task: wait/yield
Else: execute task
}
換句話說:您只需實(shí)現(xiàn)一個(gè)run()方法,該方法循環(huán)并執(zhí)行任何Runnable(或傳遞給它的任何內(nèi)容)的 run 方法。如果沒有工作可用,則該方法會休眠或等待通知。

TA貢獻(xiàn)1795條經(jīng)驗(yàn) 獲得超7個(gè)贊
首先,F(xiàn)utureTask是Runnable,所以你可以task.run();在// TODO.
其次,你根本不需要taskQueue = new BlockingQueue<FutureTask<V>>();。將您的實(shí)現(xiàn)ExecutorService作為對現(xiàn)有Executor:的補(bǔ)充
class MyExecutorService implements ExecutorService {
private final Executor executor;
public MyExecutorService(Executor executor) {
this.executor=executor;
}
public Future<?> submit(Runnable task) {
FutureTask task = new FutureTask(task);
executor.exec(task);
return task;
}
}
添加回答
舉報(bào)