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

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

代码 复制 - 运行


package net.jcip.examples; 

import java.util.*; 

/** 
 * SingleThreadRendere 
 * <p/> 
 * Rendering page elements sequentially 
 * 
 * @author Brian Goetz and Tim Peierls 
 */ 
public abstract class SingleThreadRenderer { 
    void renderPage(CharSequence source) { 
        renderText(source); 
        List<ImageData> imageData = new ArrayList<ImageData>(); 
        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<ImageInfo> 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 
 * <p/> 
 * 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<ImageInfo> imageInfos = scanForImageInfo(source); 
        Callable<List<ImageData>> task = 
                new Callable<List<ImageData>>() { 
                    public List<ImageData> call() { 
                        List<ImageData> result = new ArrayList<ImageData>(); 
                        for (ImageInfo imageInfo : imageInfos) 
                            result.add(imageInfo.downloadImage()); 
                        return result; 
                    } 
                }; 

        Future<List<ImageData>> future = executor.submit(task); 
        renderText(source); 

        try { 
            List<ImageData> 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<ImageInfo> 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 
 * <p/> 
 * 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<ImageInfo> info = scanForImageInfo(source); 
        CompletionService<ImageData> completionService = 
                new ExecutorCompletionService<ImageData>(executor); 
        for (final ImageInfo imageInfo : info) 
            completionService.submit(new Callable<ImageData>() { 
                public ImageData call() { 
                    return imageInfo.downloadImage(); 
                } 
            }); 

        renderText(source); 

        try { 
            for (int t = 0, n = info.size(); t < n; t++) { 
                Future<ImageData> 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<ImageInfo> scanForImageInfo(CharSequence s); 

    abstract void renderImage(ImageData i); 

} 

posted on 2013-11-12 17:58  Java码界探秘  阅读(95)  评论(0编辑  收藏  举报

导航