Java Reference
In-Depth Information
Listing 8.17. Result-bearing Latch Used by
ConcurrentPuzzleSolver
.
ConcurrentPuzzleSolver
does not deal well with the case where there is no solution:
if all possible moves and positions have been evaluated and no solution has been found,
solve
waits forever in the call to
getSolution
. The sequential version terminated when
it had exhausted the search space, but getting concurrent programs to terminate can some-
times be more difficult. One possible solution is to keep a count of active solver tasks and set
the solution to null when the count drops to zero, as in
Listing 8.18
.
Finding the solution may also take longer than we are willing to wait; there are several addi-
tional termination conditions we could impose on the solver. One is a time limit; this is easily
done by implementing a timed
getValue
in
ValueLatch
(which would use the timed
version of
await
), and shutting down the
Executor
and declaring failure if
getValue
times out. Another is some sort of puzzle-specific metric such as searching only up to a cer-