Java 并发编程实战学习笔记——寻找可强化的并行性

寻找可强化的并行性
1.串行执行所有任务

代码 复制 - 运行


package net.jcip.examples; 

import java.util.*; 

/** 
 * SingleThreadRendere 
 * 

* Rendering page elements sequentially * * @author Brian Goetz and Tim Peierls */ public abstract class SingleThreadRenderer { void renderPage(CharSequence source) { renderText(source); List imageData = new ArrayList(); for (ImageInfo imageInfo : scanForImageInfo(source)) imageData.add(imageInfo.downloadImage()); for (ImageData data : imageData) renderImage(data); } interface ImageData { } interface ImageInfo { ImageData downloadImage(); } abstract void renderText(CharSequence s); abstract List scanForImageInfo(CharSequence s); abstract void renderImage(ImageData i); }


2.并行执行页面渲染,和所有图片下载任务。

代码 复制 - 运行


package net.jcip.examples; 

import java.util.*; 
import java.util.concurrent.*; 
import static net.jcip.examples.LaunderThrowable.launderThrowable; 

/** 
 * FutureRenderer 
 * 

* Waiting for image download with \Future * * @author Brian Goetz and Tim Peierls */ public abstract class FutureRenderer { private final ExecutorService executor = Executors.newCachedThreadPool(); void renderPage(CharSequence source) { final List imageInfos = scanForImageInfo(source); Callable> task = new Callable>() { public List call() { List result = new ArrayList(); for (ImageInfo imageInfo : imageInfos) result.add(imageInfo.downloadImage()); return result; } }; Future> future = executor.submit(task); renderText(source); try { List imageData = future.get(); for (ImageData data : imageData) renderImage(data); } catch (InterruptedException e) { // Re-assert the thread's interrupted status Thread.currentThread().interrupt(); // We don't need the result, so cancel the task too future.cancel(true); } catch (ExecutionException e) { throw launderThrowable(e.getCause()); } } interface ImageData { } interface ImageInfo { ImageData downloadImage(); } abstract void renderText(CharSequence s); abstract List scanForImageInfo(CharSequence s); abstract void renderImage(ImageData i); }


3.并行执行页面渲染,和单个图片下载任务。

代码 复制 - 运行


package net.jcip.examples; 

import java.util.*; 
import java.util.concurrent.*; 
import static net.jcip.examples.LaunderThrowable.launderThrowable; 

/** 
 * Renderer 
 * 

* Using CompletionService to render page elements as they become available * * @author Brian Goetz and Tim Peierls */ public abstract class Renderer { private final ExecutorService executor; Renderer(ExecutorService executor) { this.executor = executor; } void renderPage(CharSequence source) { final List info = scanForImageInfo(source); CompletionService completionService = new ExecutorCompletionService(executor); for (final ImageInfo imageInfo : info) completionService.submit(new Callable() { public ImageData call() { return imageInfo.downloadImage(); } }); renderText(source); try { for (int t = 0, n = info.size(); t < n; t++) { Future f = completionService.take(); ImageData imageData = f.get(); renderImage(imageData); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (ExecutionException e) { throw launderThrowable(e.getCause()); } } interface ImageData { } interface ImageInfo { ImageData downloadImage(); } abstract void renderText(CharSequence s); abstract List scanForImageInfo(CharSequence s); abstract void renderImage(ImageData i); }

你可能感兴趣的:(多线程-并发)