package net.jcip.examples; import java.util.concurrent.atomic.*; /** * PuzzleSolver *
* Solver that recognizes when no solution exists * * @author Brian Goetz and Tim Peierls */ public class PuzzleSolverextends ConcurrentPuzzleSolver
{ PuzzleSolver(Puzzle
puzzle) { super(puzzle); } private final AtomicInteger taskCount = new AtomicInteger(0); protected Runnable newTask(P p, M m, PuzzleNode
n) { return new CountingSolverTask(p, m, n); } class CountingSolverTask extends SolverTask { CountingSolverTask(P pos, M move, PuzzleNode
prev) { super(pos, move, prev); taskCount.incrementAndGet(); } public void run() { try { super.run(); } finally { if (taskCount.decrementAndGet() == 0) solution.setValue(null); } } } }