java_ExecutorService, CompletionService - 有返回值并行工作方式

package com.demo.test3;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author QQ: 1236897
 *
 */

//工作顺序(0000)和(aaaa,bbbb,cccc,dddd)并行, (aaaa),(bbbb),(cccc),(dddd) 为串行出现
public class DemoTEST {

    public static void main(String[] args) {

        final ThreadService service = new ThreadService();

        service.renderPage();

    }

}

class ThreadService {

    private final ExecutorService executor = Executors.newCachedThreadPool();

    public void renderPage() {

        final List<String> list = new ArrayList<String>();
        list.add("aaaa");
        list.add("bbbb");
        list.add("ccccc");
        list.add("dddddd");

        Callable<List<String>> task = new Callable<List<String>>() {

            @Override
            public List<String> call() throws Exception {

                List<String> result = new ArrayList<String>();

                for (String str : list) {
                    System.out.println("input to result.....");
                    Random r = new Random();
                    double d2 = r.nextDouble() * 5;
                    Thread.sleep(1000*(int)d2);
                    result.add(str);
                }

                return result;
            }

        };

        Future<List<String>> future = executor.submit(task);
        System.out.println("000000000 - "+"=======此处和task 同时并行====================");
        try {
            List<String> res = future.get();
            for (String s : res) {
                System.out.println(s+"--"+Thread.currentThread().getName());
            }
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        executor.shutdown();

    }
}

//==============================================================================================

package com.demo.test4;

import java.util.ArrayList;
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;

/**
 * @author QQ: 1236897
 *
 */
//工作方式 - (0000)和(aaaa,bbbb,cccc,dddd)并行开始,(aaaaa)(bbbbb)(ccccc)(ddddd) 并行工作
public class DemoTest2 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ThreadService service = new ThreadService();
        service.renderPage();
    }

}

class ThreadService {

    private final ExecutorService executor = Executors.newCachedThreadPool();

    public void renderPage() {

        final List<String> list = new ArrayList<String>();
        list.add("aaaa");
        list.add("bbbb");
        list.add("ccccc");
        list.add("dddddd");

        CompletionService<String> completionService = new ExecutorCompletionService<String>(
                executor);

        for (final String s : list) {
            completionService.submit(new Callable<String>() {

                @Override
                public String call() throws Exception {
                    // TODO Auto-generated method stub
                    Random r = new Random();
                    double d2 = r.nextDouble() * 5;
                    Thread.sleep(1000 * (int) d2);
                    return s;
                }

            });
        }

        System.out.println("00000" + "==========并行开始====");

        try {
            for (int i = 0; i < list.size(); i++) {

                Future<String> str = completionService.take();
                String s = str.get();
                System.out.println(s);

            }
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

 

    原文作者:telname
    原文地址: http://www.cnblogs.com/MarchThree/p/4771052.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞