private void solveSingleThreaded(final long currentState, final BitSet walls) { initialMovesEstimate = movesRequired = Node.h(currentState); this.workers = new DFSWorker[1]; final DFSWorker dfsWorker = new DFSWorker(walls); // Add to array so GUI can poll it for the stats in real time. this.workers[0] = dfsWorker; do { if (PuzzleConfiguration.isVerbose()) { System.out.print("\nSearching paths of depth " + movesRequired + "..."); } dfsWorker.setConfig(currentState, "X", movesRequired, 0); dfsWorker.run(); if (!solved) { movesRequired += 2; } } while (running); }
private void solveMultiThreaded( final long currentState, final int numOfThreads, final BitSet walls) { if (PuzzleConfiguration.isVerbose()) { System.err.print("Creating starting positons for " + numOfThreads + " threads..."); } findStartingPositions(currentState, numOfThreads, walls); initialMovesEstimate = movesRequired = Node.h(currentState); if (PuzzleConfiguration.isVerbose()) { System.err.println("done"); } if (!solved) { final int numElements = this.queue.size(); this.workers = new DFSWorker[numElements]; for (int i = numElements - 1; i >= 0; --i) { this.workers[i] = new DFSWorker(walls); } do { if (PuzzleConfiguration.isVerbose()) { System.out.print("\nSearching paths of depth " + movesRequired + "..."); } final ExecutorService executor = Executors.newFixedThreadPool(numOfThreads); final Iterator<BFSNode> it = this.queue.iterator(); int index = 0; while (it.hasNext()) { final BFSNode node = it.next(); final String currentPath = node.getPath(); final DFSWorker worker = this.workers[index++]; worker.setConfig(node.boardConfig, currentPath, movesRequired, currentPath.length() - 1); executor.execute(worker); } executor.shutdown(); try { executor.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); } catch (final InterruptedException ie) { stop(); } if (!solved) { movesRequired += 2; } } while (running); } }