1. Executor接口
public interface Executor {
void execute(Runnable command);
}
2. 线程池
Executors的工厂方法可以返回多种线程池:
i. newFixedThreadPool(int nThread),固定大小的线程池,一直维持nThread个线程;
ii. newCachedThreadPool(),根据负载,自动调整线程池线程个数;
iii. newSingleThreadExecutor(),单线程的Executor;
iv. newScheduledThreadPool(int nThread),固定大小的线程池,可以延迟和周期性的执行任务,类似于Timer()(java1.5之后就应该很少用它了);
3. 具有生命周期的executor
ExecutorService一般为执行一组任务,它在没有shutdown前会一直等待。
awaitTermination会阻塞当前调用线程,等待线程池中的线程执行完成,或者超时,注意之前必须先调用shutdown。
public interface ExecutorService extends Executor {
void shutdown();
List<Runnable> shutdownNow();
boolean isShutdown();
boolean isTerminated();
boolean awaitTermination(long timeout, TimeUnit unit)
throws InterruptedException;
// ... additional convenience methods for task submission
}
4. Callable和Future
Executor的基本执行单元是Runner,但Runner不能返回值,也不好取消等。
接口Callable可以返回计算的值:
public interface Callable<V> {
V call() throws Exception;
}
而Future则可以管理任务的生命周期:
public interface Future<V> {
boolean cancel(boolean mayInterruptIfRunning);
boolean isCancelled();
boolean isDone();
V get() throws InterruptedException, ExecutionException,
CancellationException;
V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException,
CancellationException, TimeoutException;
}
ExecutorService.submit()方法会返回一个Future,可以用来获取返回值,或者取消任务。
也可以实例化FutureTask。
5. CompletionService,BlockingQueue和Executor的组合
你可以提交(submit)一组Callable任务,且像队列一样在可用时获取(poll或者taken)计算结果,结果都被包装在Future里。
引用书上的例子:
浏览器渲染HTML页面,先从HTML中获取图片的地址,加入到任务队列中,然后渲染文字,图片到了后,就立即显示。
package org.jamie.demo;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
class ImageData {
private final String data;
public String getData() {
return data;
}
public ImageData(String data) {
super();
this.data = data;
}
}
class ImageLoader {
private Random ran = new Random();
public ImageData load(String src) throws InterruptedException {
long loadTime = 0L;
do {loadTime = ran.nextInt(10000);} //load time between 1 secs and 10 secs.
while (1000 > loadTime);
Thread.sleep(loadTime);
return new ImageData("image data for " + src);
}
}
public class ExecutorServiceDemo {
private ImageLoader imageLoader = new ImageLoader();
public void renderHtmlPage() throws Throwable {
ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1);
List<String> imagesSources = scanImageSources();
CompletionService<ImageData> cs = new ExecutorCompletionService<ImageData>(executor);
for (final String src : imagesSources) {
cs.submit(new Callable<ImageData> () {
@Override
public ImageData call() throws Exception {
return imageLoader.load(src);
}
});
}
randerText();
for (int i = 0; i < imagesSources.size(); ++i) {
try {
Future<ImageData> future = cs.poll(2, TimeUnit.SECONDS);
if (null != future) {
ImageData data = future.get();
randerImage(data);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
throw e.getCause();
}
}
executor.shutdown();
}
private void randerImage(ImageData data) {
System.out.println("image of HTML " + data.getData());
}
private void randerText() {
System.out.println("text content of HTML");
}
private List<String> scanImageSources() {
return Arrays.asList(
"/foo.png",
"/bar.png",
"/beauty.jpg",
"/food.png",
"/sex.png",
"/ml.png",
"/boring.png");
}
public static void main(String[] args) throws Throwable {
new ExecutorServiceDemo().renderHtmlPage();
}
}
输出:
写道
text content of HTML
image of HTML image data for /beauty.jpg
image of HTML image data for /foo.png
image of HTML image data for /food.png
image of HTML image data for /bar.png
分享到:
相关推荐
Java Concurrency in Practice 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者...
David Holmes 写的关于Java Concurrency Framework的简单介绍,适用于想了解概念和入门的同学。
Java Concurrency in practice
Using the concurrency building blocks in java.util.concurrent Performance optimization dos and don'ts Testing concurrent programs Advanced topics such as atomic variables, nonblocking algorithms, ...
Java concurrency in Practice高清pdf,带目录标签,Java并发实战
java concurrency in practice
Java Concurrency in Practice JAVA并发编程实践中文版(全)第二部分
<<java并行编程>>英文版chm格式,英文名称<Java Concurrency in Practice>,一直想买这本书,但总是缺货,找到了电子版,分享给大家。 Java Concurrency in Practice By Brian Goetz, Tim Peierls, Joshua Bloch,...
Java Concurrency in Practice源码
正规PDF版本的 Java Concurrency In Practice。 经典著作,学习有益!
java concurrency programming
Java.Concurrency.in.Practice.pdf
Concurrent_Programming+Java Concurrency in Practice+langspec
Java并发编程实践(Java Concurrency in Practice) (中英版)
Java concurrency之锁_动力节点Java学院,动力节点口口相传的Java黄埔军校
Java concurrency之互斥锁_动力节点Java学院,动力节点Java学院
Separate the thread management from the rest of the application with the Executor framework Solve problems using a parallelized version of the divide and conquer paradigm with the Fork / Join ...
Java concurrency之AtomicReference原子类_动力节点Java学院整理,动力节点口口相传的Java黄埔军校