【Java】Java线程池ExecutorService中重要的方法

Java线程池ExecutorService中重要的方法

入门小站发布于 今天 14:42

ExecutorService 介绍

【Java】Java线程池ExecutorService中重要的方法

【Java】Java线程池ExecutorService中重要的方法

1. ThreadPoolExecutor

2. ScheduledThreadPoolExecutor

【Java】Java线程池ExecutorService中重要的方法

ExecutorService的创建

  • newCachedThreadPool 创建一个可缓存的线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,如果没有可以回收的,则新建线程。
  • newFixedThreadPool 创建一个定长的线程池,可控制线程最大并发数,超出的线程会在队列中等待。
  • newScheduledThreadPool 创建一个定长线程池,可以定时周期性执行任务。
  • newSingleThreadPool 创建一个单线程线程池,它只会用唯一的线程来执行任务,保证所有任务按照指定顺序来执行(FIFO,LIFO)

ExecutorService的使用

ExecutorService executorService = Executors.newFixedThreadPool(10);

executorService.execute(new Runnable() {

public void run() {

System.out.println("入门小站");

}

});

executorService.shutdown();

ExecutorService的执行方法

  • execute(Runnable)
  • submit(Runnable)
  • submit(Callable)
  • invokeAny(...)
  • invokeAll(...)

execute(Runnable) 无法获取执行结果

ExecutorService executorService = Executors.newSingleThreadExecutor();

executorService.execute(new Runnable() {

public void run() {

System.out.println("Asynchronous task");

}

});

executorService.shutdown();

submit(Runnable) 可以判断任务是否完成

Future future = executorService.submit(new Runnable() {

public void run() {

System.out.println("Asynchronous task");

}

});

future.get(); //returns null if the task has finished correctly.

submit(Callable)可以获取返回结果

Future future = executorService.submit(new Callable(){

public Object call() throws Exception {

System.out.println("Asynchronous Callable");

return "Callable Result";

}

});

System.out.println("future.get() = " + future.get());

invokeAny(...)

ExecutorService executorService = Executors.newSingleThreadExecutor();

Set<Callable<String>> callables = new HashSet<Callable<String>>();

callables.add(new Callable<String>() {

public String call() throws Exception {

return "Task 1";

}

});

callables.add(new Callable<String>() {

public String call() throws Exception {

return "Task 2";

}

});

callables.add(new Callable<String>() {

public String call() throws Exception {

return "Task 3";

}

});

String result = executorService.invokeAny(callables);

System.out.println("result = " + result);

executorService.shutdown();

invokeAll(...)

ExecutorService executorService = Executors.newSingleThreadExecutor();

Set<Callable<String>> callables = new HashSet<Callable<String>>();

callables.add(new Callable<String>() {

public String call() throws Exception {

return "Task 1";

}

});

callables.add(new Callable<String>() {

public String call() throws Exception {

return "Task 2";

}

});

callables.add(new Callable<String>() {

public String call() throws Exception {

return "Task 3";

}

});

List<Future<String>> futures = executorService.invokeAll(callables);

for(Future<String> future : futures){

System.out.println("future.get = " + future.get());

}

executorService.shutdown();

线程池ExecutorService的关闭

关注微信公众号:【入门小站】,解锁更多知识点

【Java】Java线程池ExecutorService中重要的方法

java多线程

阅读 40发布于 今天 14:42

本作品系原创,采用《署名-非商业性使用-禁止演绎 4.0 国际》许可协议

avatar

入门小站

rumenz.com

47 声望

3 粉丝

0 条评论

得票时间

avatar

入门小站

rumenz.com

47 声望

3 粉丝

宣传栏

ExecutorService 介绍

【Java】Java线程池ExecutorService中重要的方法

【Java】Java线程池ExecutorService中重要的方法

1. ThreadPoolExecutor

2. ScheduledThreadPoolExecutor

【Java】Java线程池ExecutorService中重要的方法

ExecutorService的创建

  • newCachedThreadPool 创建一个可缓存的线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,如果没有可以回收的,则新建线程。
  • newFixedThreadPool 创建一个定长的线程池,可控制线程最大并发数,超出的线程会在队列中等待。
  • newScheduledThreadPool 创建一个定长线程池,可以定时周期性执行任务。
  • newSingleThreadPool 创建一个单线程线程池,它只会用唯一的线程来执行任务,保证所有任务按照指定顺序来执行(FIFO,LIFO)

ExecutorService的使用

ExecutorService executorService = Executors.newFixedThreadPool(10);

executorService.execute(new Runnable() {

public void run() {

System.out.println("入门小站");

}

});

executorService.shutdown();

ExecutorService的执行方法

  • execute(Runnable)
  • submit(Runnable)
  • submit(Callable)
  • invokeAny(...)
  • invokeAll(...)

execute(Runnable) 无法获取执行结果

ExecutorService executorService = Executors.newSingleThreadExecutor();

executorService.execute(new Runnable() {

public void run() {

System.out.println("Asynchronous task");

}

});

executorService.shutdown();

submit(Runnable) 可以判断任务是否完成

Future future = executorService.submit(new Runnable() {

public void run() {

System.out.println("Asynchronous task");

}

});

future.get(); //returns null if the task has finished correctly.

submit(Callable)可以获取返回结果

Future future = executorService.submit(new Callable(){

public Object call() throws Exception {

System.out.println("Asynchronous Callable");

return "Callable Result";

}

});

System.out.println("future.get() = " + future.get());

invokeAny(...)

ExecutorService executorService = Executors.newSingleThreadExecutor();

Set<Callable<String>> callables = new HashSet<Callable<String>>();

callables.add(new Callable<String>() {

public String call() throws Exception {

return "Task 1";

}

});

callables.add(new Callable<String>() {

public String call() throws Exception {

return "Task 2";

}

});

callables.add(new Callable<String>() {

public String call() throws Exception {

return "Task 3";

}

});

String result = executorService.invokeAny(callables);

System.out.println("result = " + result);

executorService.shutdown();

invokeAll(...)

ExecutorService executorService = Executors.newSingleThreadExecutor();

Set<Callable<String>> callables = new HashSet<Callable<String>>();

callables.add(new Callable<String>() {

public String call() throws Exception {

return "Task 1";

}

});

callables.add(new Callable<String>() {

public String call() throws Exception {

return "Task 2";

}

});

callables.add(new Callable<String>() {

public String call() throws Exception {

return "Task 3";

}

});

List<Future<String>> futures = executorService.invokeAll(callables);

for(Future<String> future : futures){

System.out.println("future.get = " + future.get());

}

executorService.shutdown();

线程池ExecutorService的关闭

关注微信公众号:【入门小站】,解锁更多知识点

【Java】Java线程池ExecutorService中重要的方法

以上是 【Java】Java线程池ExecutorService中重要的方法 的全部内容, 来源链接: utcz.com/a/108547.html

回到顶部