线程池ExecutorService 的使用
线程池的方法
/**
* Executes the given command at some time in the future. The command
* may execute in a new thread, in a pooled thread, or in the calling
* thread, at the discretion of the {@code Executor} implementation.
*
* @param command the runnable task 运行Runnale任务,定义一个类实现Runnale,或者直接使用lambda实现()->{}
* @throws RejectedExecutionException if this task cannot be
* accepted for execution
* @throws NullPointerException if command is null
*/
void execute(Runnable command);
/**
* Submits a value-returning task for execution and returns a
* Future representing the pending results of the task. The
* Future's {@code get} method will return the task's result upon
* successful completion.
*
* <p>
* If you would like to immediately block waiting
* for a task, you can use constructions of the form
* {@code result = exec.submit(aCallable).get();}
*
* <p>Note: The {@link Executors} class includes a set of methods
* that can convert some other common closure-like objects,
* for example, {@link java.security.PrivilegedAction} to
* {@link Callable} form so they can be submitted.
*
* @param task the task to submit 定义一个类实现
* @param <T> the type of the task's result 可以指定返回值类型
* @return a Future representing pending completion of the task
* @throws RejectedExecutionException if the task cannot be
* scheduled for execution
* @throws NullPointerException if the task is null
*/
<T> Future<T> submit(Callable<T> task);
/**
* Submits a Runnable task for execution and returns a Future
* representing that task. The Future's {@code get} method will
* return the given result upon successful completion.
*
* @param task the task to submit 运行Runnale任务,定义一个类实现Runnale,或者直接使用lambda实现()->{}
* @param result the result to return 指定类型返回
* @param <T> the type of the result
* @return a Future representing pending completion of the task
* @throws RejectedExecutionException if the task cannot be
* scheduled for execution
* @throws NullPointerException if the task is null
*/
<T> Future<T> submit(Runnable task, T result);
/**
* Submits a Runnable task for execution and returns a Future
* representing that task. The Future's {@code get} method will
* return {@code null} upon <em>successful</em> completion.
*
* @param task the task to submit
* @return a Future representing pending completion of the task 返回一个任意类型的返回值
* @throws RejectedExecutionException if the task cannot be
* scheduled for execution
* @throws NullPointerException if the task is null
*/
Future<?> submit(Runnable task);
1. execute 无返回值
2. submit 有返回值
//创建 线程池
// 使用 Runnable
public class T03_Runnable {
ExecutorService executor = Executors.newFixedThreadPool(8);
List<Future> futureList = new ArrayList<>();
for (int b = 0;b<threadNum;b++){
Future<?> submit = executor.submit(() -> {
work()
});
futureList.add(submit);
}
try {
for (Future future : futureList) {
// 如果任务还没结束,会等待
if (!future.isDone()) {
System.out.println("资源还没有准备好");
}
System.out.println(future.get());
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
//
public void work(){
System.out.println("start thread "+Thread.currentThread().getName()+" thredNum is "+thredNum);
}
}
// 使用 callable
public class T03_Callable {
public static void main(String[] args) throws ExecutionException, InterruptedException {
Callable<String> callable = new Callable<String>() {
@Override
public String call() throws Exception {
return Thread.currentThread().getName()+" hello Callable";
}
};
ExecutorService executorService = Executors.newSingleThreadExecutor();
Future<String> future = executorService.submit(callable);
//
// future.get() :获取异步执行的结果,如果没有结果可用,此方法会阻塞直到异步计算完成
String s = future.get();
System.err.println(s);
executorService.shutdown();
}
}
参考:
https://blog.csdn.net/sx1119183530/article/details/79735348