java线程池和消息队列的运行流程分析
2017-05-13 22:17
351 查看
1.通过以下代码,向线程池中不断的放入线程任务
public static void main(String[] args) {
for(int i=0;i<100;i++){
new TestDriver().sendMsg( Integer.toString( i ) );
}
//new TestDriver().sendMsg(“发起一条对象” );
}
}
2:addLogMsg()方法,创建一个线程任务,交给线程池去执行
public void addLogMsg(String msg) {
//将一个线程任务交给线程池去执行
Runnable task = new AccessDBThread(msg);
threadPool.execute(task);
}
3.线程池调用AccessDBThread类的run方法。
4.若加入的任务过多。超过了线程池的服务容量,则看线程池的服务拒绝策略是什么样的。
// 管理数据库访问的线程池
@SuppressWarnings({ “rawtypes”, “unchecked” })
final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME,
TimeUnit.SECONDS, new ArrayBlockingQueue(WORK_QUEUE_SIZE), this.handler);
5.自定义的服务拒绝策略:将任务信息放入消息队列中
//一个没有能够被线程池执行的消息,重新放入消息队列中
//A handler for tasks that cannot be executed by a ThreadPoolExecutor.
final RejectedExecutionHandler handler = new RejectedExecutionHandler() {
6.定义一个定时执行的调度线程,定时的去调用一个实现了RUNNABLE接口的方法:accessBufferThread,
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(100);
@SuppressWarnings(“rawtypes”)
final ScheduledFuture taskHandler = scheduler.scheduleAtFixedRate(accessBufferThread, 0, 1, TimeUnit.SECONDS);
7.检查消息队列是否为空,不为空则将消息取出,交给线程池去处理
// 访问消息缓存的调度线程
// 查看是否有待定请求,如果有,则创建一个新的AccessDBThread,并添加到线程池中
final Runnable accessBufferThread = new Runnable() {
public static void main(String[] args) {
for(int i=0;i<100;i++){
new TestDriver().sendMsg( Integer.toString( i ) );
}
//new TestDriver().sendMsg(“发起一条对象” );
}
public void sendMsg( String msg ) { tpm.addLogMsg( msg + "记录一条日志 " );
}
2:addLogMsg()方法,创建一个线程任务,交给线程池去执行
public void addLogMsg(String msg) {
//将一个线程任务交给线程池去执行
Runnable task = new AccessDBThread(msg);
threadPool.execute(task);
}
3.线程池调用AccessDBThread类的run方法。
4.若加入的任务过多。超过了线程池的服务容量,则看线程池的服务拒绝策略是什么样的。
// 管理数据库访问的线程池
@SuppressWarnings({ “rawtypes”, “unchecked” })
final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME,
TimeUnit.SECONDS, new ArrayBlockingQueue(WORK_QUEUE_SIZE), this.handler);
5.自定义的服务拒绝策略:将任务信息放入消息队列中
//一个没有能够被线程池执行的消息,重新放入消息队列中
//A handler for tasks that cannot be executed by a ThreadPoolExecutor.
final RejectedExecutionHandler handler = new RejectedExecutionHandler() {
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { System.out.println(((AccessDBThread) r).getMsg() + "消息放入队列中重新等待执行"); //将未能处理的消息放入消息缓冲队列中 msgQueue.offer(((AccessDBThread) r).getMsg()); } };
6.定义一个定时执行的调度线程,定时的去调用一个实现了RUNNABLE接口的方法:accessBufferThread,
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(100);
@SuppressWarnings(“rawtypes”)
final ScheduledFuture taskHandler = scheduler.scheduleAtFixedRate(accessBufferThread, 0, 1, TimeUnit.SECONDS);
7.检查消息队列是否为空,不为空则将消息取出,交给线程池去处理
// 访问消息缓存的调度线程
// 查看是否有待定请求,如果有,则创建一个新的AccessDBThread,并添加到线程池中
final Runnable accessBufferThread = new Runnable() {
@Override public void run() { if (hasMoreAcquire()) { //取出消息队列的头一个信息 String msg = (String) msgQueue.poll(); //新建任务线程,将消息放入该线程中 Runnable task = new AccessDBThread(msg); //将线程给线程池执行 threadPool.execute(task); } } };
相关文章推荐
- 第二人生的源码分析(三十七)消息处理的完整流程
- 第二人生的源码分析(三十七)消息处理的完整流程
- duilib库分析: 消息流程分析
- Hadoop运行流程分析
- WM_NOTIFY消息流程实例分析
- mfc,示例 ctrlbars 程序运行流程分析
- spydroid源码分析(二):spydroid运行流程
- VxWorks消息队列分析及应用
- qte流程分析,键盘的挂载和键盘消息的传递
- android源码settings中显示所有正在运行进程流程分析
- 蔡军生先生第二人生的源码分析(三十七)消息处理的完整流程
- 主要MAP消息流程分析
- IPC消息队列使用详细分析
- Mangos源码分析(8):服务器公共组件实现之消息队列
- 开源工作流Fireflow源码分析之运行流程二
- 第二人生的源码分析(三十七)消息处理的完整流程
- IPC消息队列使用详细分析
- Wcf运行时执行流程分析
- 如何用两个栈来实现一个队列,并分析有关队列操作的运行时间。
- 基于Linux的消息队列及多线程编程实现的聊天室(二)代码分析