讀AbstractExecutorService


//他是ExecutorService的部分實現
public abstract class AbstractExecutorService implements ExecutorService

//提交一個Runnable任務給AbstractExecutorService執行返回Future
public Future<?> submit(Runnable task) {
if (task == null) throw new NullPointerException();
RunnableFuture<Void> ftask = newTaskFor(task, null);
execute(ftask);
return ftask;
}

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

//提交一個Runnable任務並指定返回值給AbstractExecutorService執行返回Future
public <T> Future<T> submit(Runnable task, T result) {
if (task == null) throw new NullPointerException();
RunnableFuture<T> ftask = newTaskFor(task, result);
execute(ftask);
return ftask;
}

//提交一個Callable任務並指定返回值給AbstractExecutorService執行返回Future
public <T> Future<T> submit(Callable<T> task) {
if (task == null) throw new NullPointerException();
RunnableFuture<T> ftask = newTaskFor(task);
execute(ftask);
return ftask;
}


//一旦有任務執行成功就返回
public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
throws InterruptedException, ExecutionException {
try {
return doInvokeAny(tasks, false, 0);
} catch (TimeoutException cannotHappen) {
assert false;
return null;
}
}

private <T> T doInvokeAny(Collection<? extends Callable<T>> tasks,
boolean timed, long nanos)
throws InterruptedException, ExecutionException, TimeoutException {
if (tasks == null)
throw new NullPointerException();
int ntasks = tasks.size();
if (ntasks == 0)
throw new IllegalArgumentException();
List<Future<T>> futures= new ArrayList<Future<T>>(ntasks);
ExecutorCompletionService<T> ecs =
new ExecutorCompletionService<T>(this);

try {

ExecutionException ee = null;
long lastTime = timed ? System.nanoTime() : 0;
Iterator<? extends Callable<T>> it = tasks.iterator();
//將任務加入到futures集合中
futures.add(ecs.submit(it.next()));
--ntasks;
int active = 1;

for (;;) {
//獲取Future
Future<T> f = ecs.poll();
//當前沒有完成的任務
if (f == null) {
//還有任務未提交
if (ntasks > 0) {
--ntasks;
//再提交一個任務
futures.add(ecs.submit(it.next()));
++active;
}
//沒有活動的任務重新循環
else if (active == 0)
break;
//如果有超時
else if (timed) {
f = ecs.poll(nanos, TimeUnit.NANOSECONDS);
//超時拋出異常
if (f == null)
throw new TimeoutException();
long now = System.nanoTime();
nanos -= now - lastTime;
lastTime = now;
}
else
//都沒有則阻塞獲取Future
f = ecs.take();
}
if (f != null) {
--active;
try {
return f.get();
} catch (ExecutionException eex) {
ee = eex;
} catch (RuntimeException rex) {
ee = new ExecutionException(rex);
}
}
}

if (ee == null)
ee = new ExecutionException();
throw ee;

} finally {
for (Future<T> f : futures)
//嘗試取消完成的任務
f.cancel(true);
}
}


//一旦有任務執行成功就返回,超時拋出異常
public <T> T invokeAny(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
return doInvokeAny(tasks, true, unit.toNanos(timeout));
}


//執行所有Futrue返回完成任務的Future列表
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException {
if (tasks == null)
throw new NullPointerException();
List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
boolean done = false;
try {
for (Callable<T> t : tasks) {
RunnableFuture<T> f = newTaskFor(t);
futures.add(f);
execute(f);
}
for (Future<T> f : futures) {
//如果任務還沒有完成
if (!f.isDone()) {
try {
//獲取
f.get();
} catch (CancellationException ignore) {
} catch (ExecutionException ignore) {
}
}
}
done = true;
return futures;
} finally {
//如果沒有任務完成,嘗試取消Future
if (!done)
for (Future<T> f : futures)
f.cancel(true);
}
}


//執行所有任務返回所有未超時並且執行了的任務
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit)
throws InterruptedException {
if (tasks == null || unit == null)
throw new NullPointerException();
long nanos = unit.toNanos(timeout);
List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
boolean done = false;
try {
for (Callable<T> t : tasks)
futures.add(newTaskFor(t));

long lastTime = System.nanoTime();


Iterator<Future<T>> it = futures.iterator();
while (it.hasNext()) {
execute((Runnable)(it.next()));
long now = System.nanoTime();
nanos -= now - lastTime;
lastTime = now;
//超時了直接返回
if (nanos <= 0)
return futures;
}

for (Future<T> f : futures) {
//如果任務還爲完成
if (!f.isDone()) {
//超時了直接返回
if (nanos <= 0)
return futures;
try {
f.get(nanos, TimeUnit.NANOSECONDS);
} catch (CancellationException ignore) {
} catch (ExecutionException ignore) {
} catch (TimeoutException toe) {
//超時異常直接返回
return futures;
}
long now = System.nanoTime();
nanos -= now - lastTime;
lastTime = now;
}
}
done = true;
return futures;
} finally {
//所有任務並沒有全部返回(可能拋出異常等)
if (!done)
for (Future<T> f : futures)
f.cancel(true);
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章