《Java Concurrency in Practice》ch6 Task Execution
2012-03-20 21:28
483 查看
3个notes,
1. don't use Thread, use Executor
Executor may be a simple interface, but it forms the basis for a flexible and powerful framework for asynchronous task execution that supports a wide variety of task execution policies. It provides a standard means of decoupling task submission from task
execution, describing tasks with Runnable. The Executor implementations also provide lifecycle support and hooks for adding statistics gathering, application management, and monitoring.
Executor is based on the producer-consumer pattern, where activities that submit tasks are the producers (producing units of work to be done) and the threads that execute tasks are the consumers (consuming those units of work).
2. don't use Timer, use ScheduledThreadPoolExecutor (thread leakage)
thread leakage 例子,
Question: main will exit after how many seconds?
1) 1
2) 51
Run Result:
Answer: 1 second
3. don't user Runnable, use Callable/Future
Finding Exploitable Parallelism 找出可以利用的并行性
The real performance payoff of dividing a program's workload into tasks comes when there are a large number of independent, homogeneous tasks that can be processed concurrently.
在异构任务并行化中存在的局限:只有当大量相互独立且同构的任务可以并发进行处理时,才能体现出将程序的工作负载分配到多个任务中带来的真正性能提升。
1. don't use Thread, use Executor
Executor may be a simple interface, but it forms the basis for a flexible and powerful framework for asynchronous task execution that supports a wide variety of task execution policies. It provides a standard means of decoupling task submission from task
execution, describing tasks with Runnable. The Executor implementations also provide lifecycle support and hooks for adding statistics gathering, application management, and monitoring.
Executor is based on the producer-consumer pattern, where activities that submit tasks are the producers (producing units of work to be done) and the threads that execute tasks are the consumers (consuming those units of work).
2. don't use Timer, use ScheduledThreadPoolExecutor (thread leakage)
thread leakage 例子,
import java.util.Timer; import java.util.TimerTask; /** * thread leakage * */ public class OutOfTime { public static void main(String [] args) throws InterruptedException{ Timer timer = new Timer(); timer.schedule(new ThrowTask(), 1); Thread.sleep(1000); timer.schedule(new ThrowTask(), 1); Thread.sleep(50000); } static class ThrowTask extends TimerTask { public void run() { throw new RuntimeException(); } } }
Question: main will exit after how many seconds?
1) 1
2) 51
Run Result:
Exception in thread "Timer-0" java.lang.RuntimeException at OutOfTime$ThrowTask.run(OutOfTime.java:21) at java.util.TimerThread.mainLoop(Timer.java:512) at java.util.TimerThread.run(Timer.java:462) Exception in thread "main" java.lang.IllegalStateException: Timer already cancelled. at java.util.Timer.sched(Timer.java:354) at java.util.Timer.schedule(Timer.java:170) at OutOfTime.main(OutOfTime.java:15)
Answer: 1 second
3. don't user Runnable, use Callable/Future
Finding Exploitable Parallelism 找出可以利用的并行性
The real performance payoff of dividing a program's workload into tasks comes when there are a large number of independent, homogeneous tasks that can be processed concurrently.
在异构任务并行化中存在的局限:只有当大量相互独立且同构的任务可以并发进行处理时,才能体现出将程序的工作负载分配到多个任务中带来的真正性能提升。
相关文章推荐
- Java并发编程学习——《Java Concurrency in Practice》学习笔记 5.基础构建模块
- Java Concurrency in Practice中对象锁重入问题的理解
- Java并发编程实践评价(Java Concurrency in Practice)
- 《Java Concurrency in Practice》 学习笔记--第一章:引言
- 《Java Concurrency in Practice》 学习笔记--第二章:线程安全
- Java并发编程实践(Java concurrency in practice)在线示例学习
- Java Concurrency In Practice - Chapter 1 Introduction
- Java并发编程学习——《Java Concurrency in Practice》学习笔记 并发技巧清单
- Java Concurrency in Practice——读书笔记
- Java Concurrency in Practice :基础知识(线程同步)
- 《Java Concurrency in Practice》之Disruptor简单类图
- 《Java Concurrency in Practice》ch5 Building Blocks
- [Java Concurrency in Practice]第五章 基础构建模块
- Java并发编程学习——《Java Concurrency in Practice》学习笔记 6.任务执行
- Java Concurrency In Practice -Chapter 2 Thread Safety
- 《Java Concurrency in Practice》之初识并发编程
- 《Java Concurrency in Practice》之加锁机制(Locking)
- java concurrency in practice - 2
- java 并发编程实践( java concurrency in practice )
- Java Concurrency in Practice :基础知识(正确的同步 - 组合)