// 执行任务 void execute(Runnable command); // 提交任务 task,用返回值 Future 获得任务执行结果Future submit(Callable task); // 提交 tasks 中所有任务 List > invokeAll(Collection extends Callable > tasks) throws InterruptedException; // 提交 tasks 中所有任务,带超时时间 List > invokeAll(Collection extends Callable > tasks, long timeout, TimeUnit unit) throws InterruptedException; // 提交 tasks 中所有任务,哪个任务先成功执行完毕,返回此任务执行结果,其它任务取消 T invokeAny(Collection extends Callable > tasks) throws InterruptedException, ExecutionException; // 提交 tasks 中所有任务,哪个任务先成功执行完毕,返回此任务执行结果,其它任务取消,带超时时间 T invokeAny(Collection extends Callable > tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;
1.1 execute 执行任务
示例代码:
package com.tian;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
@Slf4j(topic = "TestThreadPoolExecutors")
public class TestThreadPoolExecutors {
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(2, new ThreadFactory() {
// 自增的原子整数
private AtomicInteger number = new AtomicInteger(1);
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "my_pool_thread: " + number.getAndIncrement());
}
});
pool.execute(() -> {
log.debug("线程01执行了.");
});
pool.execute(() -> {
log.debug("线程02执行了.");
});
pool.execute(() -> {
log.debug("线程03执行了.");
});
pool.execute(() -> {
log.debug("线程04执行了.");
});
}
}
运行结果:
1.2 submit 提交任务 获得任务执行结果
package com.tian;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
@Slf4j(topic = "c.TestSubmit")
public class TestSubmit {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService pool = Executors.newFixedThreadPool(2);
// 主线程获取 future 的返回结果
method1(pool);
}
private static void method1(ExecutorService pool) throws InterruptedException, ExecutionException {
Future future = pool.submit(() -> {
log.debug("running");
Thread.sleep(1000);
return "成功获取future的返回结果";
});
log.debug("{}", future.get());
}
}
运行结果:
1.3 invokeAll 提交所有任务 获取返回结果
示例代码:
package com.tian;
import lombok.extern.slf4j.Slf4j;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
@Slf4j(topic = "c.TestSubmit")
public class TestSubmit {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService pool = Executors.newFixedThreadPool(1);
method2(pool);
}
private static void method2(ExecutorService pool) throws InterruptedException {
List> futures = pool.invokeAll(Arrays.asList(
() -> {
log.debug("begin");
Thread.sleep(1000);
return "成功获取第一个线程返回的结果";
},
() -> {
log.debug("begin");
Thread.sleep(500);
return "成功获取第二个线程返回的结果";
},
() -> {
log.debug("begin");
Thread.sleep(2000);
return "成功获取第三个线程返回的结果";
}
));
futures.forEach(task -> {
try {
log.debug("{}", task.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
});
}
}
运行结果:
1.4 invokeAny 提交 所有任务 一旦有任务先成功执行完毕,则返回此任务执行结果,其它任务取消
示例代码:
package com.tian;
import lombok.extern.slf4j.Slf4j;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Slf4j(topic = "c.TestSubmit")
public class TestSubmit {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService pool = Executors.newFixedThreadPool(1);
method3(pool);
}
private static void method3(ExecutorService pool) throws InterruptedException, ExecutionException {
String result = pool.invokeAny(Arrays.asList(
() -> {
Thread.sleep(500);
log.debug("begin 1");
log.debug("end 1");
return "成功获取第一个线程执行的结果";
},
() -> {
Thread.sleep(1000);
log.debug("begin 2");
log.debug("end 2");
return "成功获取第二个线程执行的结果";
},
() -> {
Thread.sleep(1500);
log.debug("begin 3");
log.debug("end 3");
return "成功获取第三个线程执行的结果";
}
));
log.debug("{}", result);
}
}
运行结果:



