从java中的executor返回结果

时间:2015-06-02 05:48:26

标签: java multithreading

我需要一些帮助来检索执行者的结果。我创建工人的地方:

public static void method()
    {
        double bestProb = 0;
        double best p1 = 10000;
        double best p2 = 10000;

        executor = Executors.newFixedThreadPool(5);
        List<Future<Double>> futures = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            double p1 = Math.pow(2, i);
            for (int j = 5; j < 15; j++) {
                double p2 = Math.pow(2, j);
                Callable<Double> worker = new ProbabilityCalculator(p1, p2,);
                futures.add(executor.submit(worker));
                }
            }

        for (Future<Double> future : futures) {
             double prob = future.get();
             prob /= 100;   

                if (prob > bestProb) {
                    bestProb = prob;
                    bestp1 = p1;
                    bestp2 = p2;
                }
}

        System.out.println("Best prob: " + bestProb + ", for: " + bestp1 + ", " + bestp2);
    }

我要返回的ProbabilityCalculator类,我必须使用参数methodp1p2中处理这一概率,从而导致此概率。

public class ProbabilityCalculator implements Callable<Double> {
    private double p1;
    private double p2;
    private double probability;

    private void processCommand() {
     // perform operations
     // here I compute the probability each time
    }          
    public double returnProbability() {
         return this.probability;
    }

    @Override
    public void call() {
        processCommand();
    } 
}

1 个答案:

答案 0 :(得分:2)

(当问题出现在修订版1时,提供了这个答案.OP正在编辑问题以回答答案,这使答案变得不那么有意义 - 而不是我试着跟上编辑,请参考修订版1,看看这是什么回应......)

如果使用Callable而不是Runnable,则可以使用执行程序返回的未来来获取返回值。如果只是想要概率,你可以使用Callable<Double>这样:

List<Future<Double>> futures = new ArrayList<>();
for (int i = 0; i < 10; i++) {
    double p1 = Math.pow(2, i);
    for (int j = 5; j < 15; j++) {
        double p2 = Math.pow(2, j);
        Callable<Double> worker = new ProbabilityCalculator(p1, p2);
        futures.add(executor.submit(worker));           
    }
}
// Do whatever else you want to here...

// Now wait for the results and handle them appropriately.
// Note the exceptions that can be thrown...
for (Future<Double> future : futures) {
    double value = future.get();
    ...
}

重要的是,Runnable不会返回值,而Callable会返回值。

听起来你也需要p1p2值,你应该将其封装在结果类型中,这样你就可以实现Callable<ProbabilityResult>,例如< / p>

public class ProbabilityCalculator implements Callable<ProbabilityResult> {
    private final double p1, p2;

    public ProbabilityCalculator(double p1, double p2) {
        this.p1 = p1;
        this.p2 = p2;
    }

    public ProbabilityResult call() {
        double probabilty = ...;
        return new ProbabilityResult(p1, p2, probability);
    }
}

(其中ProbabilityResult只是一个简单的数据传输类,实际上 - 你可以只使用带有三个值的double[],但那会非常不愉快。 。)

现在你可以使用:

double bestProbability = -1;
double bestP1 = 0, bestP2 = 0;
for (Future<ProbabilityResult> future : futures) {
    ProbabilityResult result = future.get();
    if (result.getProbability() > bestProbability) {
        bestProbability = result.getProbability();
        bestP1 = result.getP1();
        bestP2 = result.getP2();
    }
}

请注意,我已将WorkerThread更改为ProbabilityCalculator - 您应 在此处创建Thread子类的实例;关键在于遗嘱执行人处理实际的线程;你只需要提供执行的任务。如果您的WorkerThread课程实际上并未延长Thread,则应将其重命名为不那么具误导性的课程。