Java多线程实用篇之实现Callable接口,处理带有返回值的线程!

版权声明:转载随意,希望你戒骄戒躁,解决问题的同时,记在心里! https://blog.csdn.net/shasiqq/article/details/53289133
package cn.com.jd.test.volatiletest;

import java.util.Random;
import java.util.concurrent.*;

/**
 * Created by jihaixiao on 2016/11/18.
 */
public class ThreadPoolCallExecute {

    private static ExecutorService threadPool = Executors.newFixedThreadPool(5);

    private static CountDownLatch counter = new CountDownLatch(1);

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        try {
            Thread A = new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("逻辑A");
                    counter.countDown();
                }
            });
            A.start();

            for (int i=0;i<5;i++){
                Future<Integer> future = threadPool.submit(new Caller(12));
                System.out.println(future.get());
            }
        } finally {
            threadPool.shutdown();
        }

    }


    static class Caller implements Callable{

        private int param;

        public Caller(int param) {
            this.param = param;
        }


        @Override
        public Object call() throws Exception {
            counter.await();
            System.out.println("逻辑B");
            param = param + new Random().nextInt(20);
            return param;
        }
    }

}

PS:这种方式可以处理带有返回值的需求!

展开阅读全文

没有更多推荐了,返回首页