(转)java并发编程--Executor框架
本文转自https://www.cnblogs.com/MOBIN/p/5436482.html
java并发编程--Executor框架
只要用到线程,就可以使用executor.,在开发中如果需要创建线程可优先考虑使用Executor,并非只有线程池可以使用executor,单线程也可以使用executor,因为executor提供了很多其他功能,包括线程状态,生命周期的管理。故,只要用到线程,就可以使用executor.
只要用到线程,就可以使用executor.,在开发中如果需要创建线程可优先考虑使用Executor
只要用到线程,就可以使用executor.在开发中如果需要创建线程可优先考虑使用Executor
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
摘要:



public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) //后两个参数为可选参数

public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
//使用一个基于FIFO排序的阻塞队列,在所有corePoolSize线程都忙时新任务将在队列中等待
new LinkedBlockingQueue<Runnable>());
}

public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
//corePoolSize和maximumPoolSize都等于,表示固定线程池大小为1
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}




1 public class HeartBeat {
2 public static void main(String[] args) {
3 ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);
4 Runnable task = new Runnable() {
5 public void run() {
6 System.out.println("HeartBeat.........................");
7 }
8 };
9 executor.scheduleAtFixedRate(task,5,3, TimeUnit.SECONDS); //5秒后第一次执行,之后每隔3秒执行一次
10 }
11 }

HeartBeat....................... //5秒后第一次输出
HeartBeat....................... //每隔3秒输出一个
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
//使用同步队列,将任务直接提交给线程
new SynchronousQueue<Runnable>());
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public class ThreadPoolTest { public static void main(String[] args) throws InterruptedException { ExecutorService threadPool = Executors.newCachedThreadPool(); //线程池里面的线程数会动态变化,并可在线程线被移除前重用 for ( int i = 1 ; i <= 3 ; i ++) { final int task = i; //10个任务 //TimeUnit.SECONDS.sleep(1); threadPool.execute( new Runnable() { //接受一个Runnable实例 public void run() { System.out.println( "线程名字: " + Thread.currentThread().getName() + " 任务名为: " +task); } }); } } } |
线程名字: pool-1-thread-1 任务名为: 1
线程名字: pool-1-thread-2 任务名为: 2
线程名字: pool-1-thread-3 任务名为: 3
线程名字: pool-1-thread-1 任务名为: 1
线程名字: pool-1-thread-1 任务名为: 2
线程名字: pool-1-thread-1 任务名为: 3
1
2
3
4
5
6
7
8
9
10
11
|
public class CallableAndFuture { public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService executor = Executors.newSingleThreadExecutor(); Future<String> future = executor.submit( new Callable<String>() { //接受一上callable实例 public String call() throws Exception { return "MOBIN" ; } }); System.out.println( "任务的执行结果:" +future.get()); } } |
任务的执行结果:MOBIN
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class CompletionServiceTest { public static void main(String[] args) throws InterruptedException, ExecutionException { ExecutorService executor = Executors.newFixedThreadPool( 10 ); //创建含10.条线程的线程池 CompletionService completionService = new ExecutorCompletionService(executor); for ( int i = 1 ; i <= 10 ; i ++) { final int result = i; completionService.submit( new Callable() { public Object call() throws Exception { Thread.sleep( new Random().nextInt( 5000 )); //让当前线程随机休眠一段时间 return result; } }); } System.out.println(completionService.take().get()); //获取执行结果 } } |
3
(转)java并发编程--Executor框架的更多相关文章
- Java 并发编程——Executor框架和线程池原理
Eexecutor作为灵活且强大的异步执行框架,其支持多种不同类型的任务执行策略,提供了一种标准的方法将任务的提交过程和执行过程解耦开发,基于生产者-消费者模式,其提交任务的线程相当于生产者,执行任务 ...
- Java 并发编程——Executor框架和线程池原理
Java 并发编程系列文章 Java 并发基础——线程安全性 Java 并发编程——Callable+Future+FutureTask java 并发编程——Thread 源码重新学习 java并发 ...
- java并发编程-Executor框架
Executor框架是指java 5中引入的一系列并发库中与executor相关的一些功能类,其中包括线程池,Executor,Executors,ExecutorService,Completion ...
- Java 并发编程 Executor 框架
本文部分摘自<Java 并发编程的艺术> Excutor 框架 1. 两级调度模型 在 HotSpot VM 的线程模型中,Java 线程被一对一映射为本地操作系统线程.在上层,Java ...
- Java并发编程-Executor框架(转)
本文转自http://blog.csdn.net/chenchaofuck1/article/details/51606224 感谢作者 我们在传统多线程编程创建线程时,常常是创建一些Runnable ...
- Java并发编程-Executor框架集
Executor框架集对线程调度进行了封装,将任务提交和任务执行解耦. 它提供了线程生命周期调度的所有方法,大大简化了线程调度和同步的门槛. Executor框架集的核心类图如下: 从上往下,可以很清 ...
- java并发编程--Executor框架(一)
摘要: Eexecutor作为灵活且强大的异步执行框架,其支持多种不同类型的任务执行策略,提供了一种标准的方法将任务的提交过程和执行过程解耦开发,基于生产者-消费者模式,其提交任务的线程 ...
- java并发编程-Executor框架 + Callable + Future
from: https://www.cnblogs.com/shipengzhi/articles/2067154.html import java.util.concurrent.*; public ...
- java 并发编程 Executor框架
http://blog.csdn.net/chenchaofuck1/article/details/51606224 demo package executor; import java.util. ...
随机推荐
- 设计模式及Python实现
设计模式是什么? Christopher Alexander:“每一个模式描述了一个在我们周围不断重复发生的问题,以及该问题的解决方案的核心.这样你就能一次又一次地使用该方案而不必做重复劳动.” 设计 ...
- 【前端vue开发】vue知识点超链接学习笔记
1.如何去除vue项目中的 # --- History模式: https://www.cnblogs.com/zhuzhenwei918/p/6892066.html 2.三分钟教你写个Vue组件: ...
- MySQL 数据库性能优化之SQL优化【转】
优化目标 减少 IO 次数IO永远是数据库最容易瓶颈的地方,这是由数据库的职责所决定的,大部分数据库操作中超过90%的时间都是 IO 操作所占用的,减少 IO 次数是 SQL 优化中需要第一优先考虑, ...
- Little-endian和Big-endian
谈到字节序的问题,必然牵涉到两大CPU派系.那就是Motorola的PowerPC系列CPU和Intel的x86系列CPU.PowerPC系列采用big endian方式存储数据,而x86系列则采用l ...
- python程序后台运行的实现
后台运行work()方法. work.py # -*- coding:utf-8 -*- def work(): print "running" import time time. ...
- Matlab保存uint16格式文件的相关注意事项
在matlab中,我们常使用imshow()函数来显示图像,而此时的图像矩阵可能经过了某种运算.在matlab中,为了保证精度,经过了运算的图像矩阵I其数据类型会从unit8型变成double型.如果 ...
- UVALive - 6709
二维线段树单点修改板子题 #include<bits/stdc++.h> #define LL long long #define fi first #define se second # ...
- 【转】HTML5 API——无刷新更新地址 history.pushState/replaceState 方法
(window.location)在通过JavaScript更改以后,浏览器都会通过刷新来到达你更改后的URL(location的意思就是位 置..) 而在JavaScript MVC开始流行之后,通 ...
- 024.Zabbix告警等级机制
一 等级告警 告警升级可以对告警结果按自定义的时间段进行进行消息发送,并执行命令,形成一个梯度的告警处理. 二 按时间自定义告警梯度 2.1 添加时间发生的时间戳和发送时间 2.2 设置三个等级梯度 ...
- JSP中的指令概述和示例
一.JSP——Java server page :java服务端的页面,这是属于一个后端技术 1.前端技术: html.css.javascript 2.后端技术: java语言.框架(mybatis ...