来自runnable中的最终变量的空指针异常

时间:2015-06-30 04:02:26

标签: java nullpointerexception java-8

我有代码在给定问题上运行算法组合,然后只要一个算法找到问题的答案,程序就会继续。投资组合中的其他算法获得自愿信号终止,主要执行线程继续。

此代码的一个用户正在向我发送一个带有NullPointerException的堆栈跟踪 “resultReference.set(solverResult);” 从下面的代码中可以看出,resultReference是一个最终变量,并立即初始化。我看不出它怎么可能变成空的。我花了很长时间试图在我的最终重现问题无济于事。用户堆栈跟踪中的行号与我代码中的行号匹配。用户报告在3个不同的场合看到了错误,但很少(每次问题解决时都不会发生这种情况),所以可能是某种竞争条件。这是jdk 1.8_25。

我是否正确地假设这个错误应该是不可能的,因为变量是最终的?我不知道该怎么做这个堆栈跟踪,并希望有些让人放心,这应该是不可能的。

public class ParallelSolver {

private final ListeningExecutorService executorService;
private final AtomicReference<Throwable> error;
private final List<Solver> solvers;
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ParallelSolver.class);

public ParallelSolver(int threadPoolSize, List<Solvers> solvers) {
    executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(threadPoolSize));
    error = new AtomicReference<>();
    this.solvers = solvers;
}

public SolverResult solve(Problem p) {
    final AtomicReference<SolverResult> resultReference = new AtomicReference<>();
    final List<Future> futures = new ArrayList<>();
    final Semaphore workDone = new Semaphore(0);
    try {
        // Submit one job per each solver in the portfolio
        solvers.forEach(solver -> {
            final ListenableFuture<Void> future = executorService.submit(() -> {
                SolverResult solverResult = solver.solve(p);
                if (solverResult.isConclusive()) {
                    log.debug("Signalling the blocked thread to wake up!");
                     // NPE HERE ON THIS LINE
                    resultReference.set(solverResult);
                    workDone.release(solvers.size());
                }
                log.debug("Releasing a single permit as the work for this thread is done.");
                workDone.release(1);
                log.debug("Job ending...");
                return null;
            });
            futures.add(future);
            Futures.addCallback(future, new FutureCallback<Void>() {
                @Override
                public void onSuccess(Void result) {

                }

                @Override
                public void onFailure(Throwable t) {
                    if (t instanceof CancellationException) {
                        return;
                    }
                    error.compareAndSet(null, t);
                    // Wake up the main thread (if it's still sleeping)
                    workDone.release(solvers.size());
                }
            });
        });
        // Wait for a thread to complete solving and signal you, or all threads to timeout
        log.debug("Main thread going to sleep");
        workDone.acquire(solvers.size());
        log.debug("Main thread waking up, checking for errors then cancelling futures");
        checkForErrors();
        // cancel any still to be launched futures
        futures.forEach(future -> future.cancel(false));
        log.debug("Returning now");
        return resultReference.get() == null ? SolverResult.createTimeoutResult() : resultReference.get();
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted while running parallel job", e);
    }
}

/**
 * We want a fail-fast policy, but java executors aren't going to throw the exception on the main thread.
 * We can't call Future.get() and check for errors, because that might block.
 * So we set a variable when an error occurs, and check it here.
 */
private void checkForErrors() {
    if (error.get() != null) {
        log.error("Error occured while executing a task", error.get());
        throw new RuntimeException("Error occurred while executing a task", error.get());
    }
}

1 个答案:

答案 0 :(得分:1)

这几乎是你想要的使用akka:

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import scala.concurrent.Await;
import scala.concurrent.Future;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.pattern.Patterns;
import akka.util.Timeout;


public class AlgorithmTester extends UntypedActor
{
    public AlgorithmTester(){}

    public static class RegisterResultListener
    {

    }

    public static class Result
    {
        final double result;
        public Result(double result)
        {
            this.result = result;
        }
    }

    public static interface Algorithmable
    {
        public Result solve();
    }

    @SuppressWarnings("serial")
    public static class AlgorithmsToTest extends ArrayList<Algorithmable> {
    }

    public static class AlgorithmRunner extends UntypedActor
    {

        public AlgorithmRunner(){}

        @Override
        public void onReceive(Object msg) throws Exception
        {
            if (msg instanceof Algorithmable)
            {
                Algorithmable alg = (Algorithmable) msg;
                getSender().tell(alg.solve(), getSelf());
            }
        }
    }

    List<ActorRef> runners = new ArrayList<ActorRef>();
    List<ActorRef> resultListeners = new ArrayList<ActorRef>();

    @Override
    public void onReceive(Object msg) throws Exception
    {

        if (msg instanceof RegisterResultListener)
        {
            resultListeners.add(getSender());
        }
        else if (msg instanceof AlgorithmsToTest)
        {
            AlgorithmsToTest algorithms = (AlgorithmsToTest) msg;
            for (Algorithmable algorithm : algorithms)
            {
                ActorRef runner = getContext().actorOf(Props.create(AlgorithmRunner.class));
                runners.add(runner);
                runner.tell(algorithm, getSelf());
            }
            getSelf().tell(new RegisterResultListener(), getSender());
        }
        else if (msg instanceof Result)
        {
            for (ActorRef runner : runners)
            {
                getContext().stop(runner);
            }
            runners.clear();

            for (ActorRef l : resultListeners)
            {
                l.tell(msg, getSelf());
            }
        }
    }


    public static void main(String[] args)
    {
        ActorSystem system = ActorSystem.create("AlogrithmTest");
        ActorRef tester = system.actorOf(Props.create(AlgorithmTester.class), "algorithmTest");

        Algorithmable a1 = new Algorithmable()
       {

            public Result solve() {
                try {
                    Thread.sleep(7000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return new Result(1100.0);
                }
        };

        Algorithmable a2 = new Algorithmable()
       {

            public Result solve() {
                try {
                    Thread.sleep(6000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return new Result(330.0);
                }
        };

       Algorithmable a3 = new Algorithmable()
       {

            public Result solve() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return new Result(1000);
                }
        };

        AlgorithmsToTest algorithmsToTest = new AlgorithmsToTest();
        algorithmsToTest.add(a1);
        algorithmsToTest.add(a2);
        algorithmsToTest.add(a3);

        Timeout t = new Timeout(5, TimeUnit.SECONDS);

        Future<Object> future = Patterns.ask(tester, algorithmsToTest, 100000);
        try {
            Result response = (Result)Await.result(future, t.duration());
            System.out.println(response.result);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("Continuing on");
        system.terminate();
        System.out.println("Terminated");
    }
}

然而,在他们处理消息时,akka无法杀死演员,你会注意到这个程序在演员处理其他算法时继续执行,即使第一个答案已经找到。 杀死线程永远不会很好,所以你的问题没有很好的解决方案。您可以在我猜的主方法结束时在System.exit(0)上进行标记,或者在算法中某处有一个可怕的原子变量,如果它们正在迭代并抛出异常,或者将它们作为线程并杀死它们,所有这些都没有非常好 ): 我个人如果你能逃脱它,我会使用System.exit(0)。

编辑:好的,感谢你无缘无故地投票。看看这是你想要做的替代代码,没有atmoic变量,原子变量和锁,所有其他东西都是非常危险的容易出错,这是一个更清晰的答案,downvoting这是绝对的废话,所有需要改变这个代码以匹配你想要的只是结果,或算法接口,并提供你想要的所有实现这完全是你问。事实上,你只是在没有评论的情况下对此进行投票,这意味着你不知道如何使用stackoverflow。如果这个downvote推断你认为原子变量比基于actor的模型更受欢迎,那么我建议你做一些阅读。 Asker甚至没有为空指针异常提供堆栈跟踪,所以不可能直接解决这个问题,nextime需要花费0.01%的时间来写下这个答案,以便在downvote之前写一条评论。