Java 并发编程实战学习笔记——路径查找类型并行任务的终止
2013-11-12 18:00
996 查看
1.该类问题的递归串行算法(深度优先遍历)
代码
复制 -
运行
2.该类问题的并行算法(CountDownLatch 用作一个简单的开/关锁存器)
广度优先算法
代码
复制 -
运行
代码
复制 -
运行
该段代码有个问题,就是不能够感知到该问题没有答案。
因此可以改为,可以感知到任务不存在的解决中
代码
复制 -
运行
代码
复制 -
运行
package net.jcip.examples; import java.util.*; /** * SequentialPuzzleSolver * <p/> * Sequential puzzle solver * * @author Brian Goetz and Tim Peierls */ public class SequentialPuzzleSolver <P, M> { private final Puzzle<P, M> puzzle; private final Set<P> seen = new HashSet<P>(); public SequentialPuzzleSolver(Puzzle<P, M> puzzle) { this.puzzle = puzzle; } public List<M> solve() { P pos = puzzle.initialPosition(); return search(new PuzzleNode<P, M>(pos, null, null)); } private List<M> search(PuzzleNode<P, M> node) { if (!seen.contains(node.pos)) { seen.add(node.pos); if (puzzle.isGoal(node.pos)) return node.asMoveList(); for (M move : puzzle.legalMoves(node.pos)) { P pos = puzzle.move(node.pos, move); PuzzleNode<P, M> child = new PuzzleNode<P, M>(pos, move, node); List<M> result = search(child); if (result != null) return result; } } return null; } }
2.该类问题的并行算法(CountDownLatch 用作一个简单的开/关锁存器)
广度优先算法
代码
复制 -
运行
package net.jcip.examples; import java.util.concurrent.*; /** * ValueLatch * <p/> * Result-bearing latch used by ConcurrentPuzzleSolver * * @author Brian Goetz and Tim Peierls */ @ThreadSafe public class ValueLatch <T> { @GuardedBy("this") private T value = null; private final CountDownLatch done = new CountDownLatch(1); public boolean isSet() { return (done.getCount() == 0); } public synchronized void setValue(T newValue) { if (!isSet()) { value = newValue; done.countDown(); } } public T getValue() throws InterruptedException { done.await(); synchronized (this) { return value; } } }
代码
复制 -
运行
package net.jcip.examples; import java.util.*; import java.util.concurrent.*; /** * ConcurrentPuzzleSolver * <p/> * Concurrent version of puzzle solver * * @author Brian Goetz and Tim Peierls */ public class ConcurrentPuzzleSolver <P, M> { private final Puzzle<P, M> puzzle; private final ExecutorService exec; private final ConcurrentMap<P, Boolean> seen; protected final ValueLatch<PuzzleNode<P, M>> solution = new ValueLatch<PuzzleNode<P, M>>(); public ConcurrentPuzzleSolver(Puzzle<P, M> puzzle) { this.puzzle = puzzle; this.exec = initThreadPool(); this.seen = new ConcurrentHashMap<P, Boolean>(); if (exec instanceof ThreadPoolExecutor) { ThreadPoolExecutor tpe = (ThreadPoolExecutor) exec; tpe.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy()); } } private ExecutorService initThreadPool() { return Executors.newCachedThreadPool(); } public List<M> solve() throws InterruptedException { try { P p = puzzle.initialPosition(); exec.execute(newTask(p, null, null)); // block until solution found PuzzleNode<P, M> solnPuzzleNode = solution.getValue(); return (solnPuzzleNode == null) ? null : solnPuzzleNode.asMoveList(); } finally { exec.shutdown(); } } protected Runnable newTask(P p, M m, PuzzleNode<P, M> n) { return new SolverTask(p, m, n); } protected class SolverTask extends PuzzleNode<P, M> implements Runnable { SolverTask(P pos, M move, PuzzleNode<P, M> prev) { super(pos, move, prev); } public void run() { if (solution.isSet() || seen.putIfAbsent(pos, true) != null) return; // already solved or seen this position if (puzzle.isGoal(pos)) solution.setValue(this); else for (M m : puzzle.legalMoves(pos)) exec.execute(newTask(puzzle.move(pos, m), m, this)); } } }
该段代码有个问题,就是不能够感知到该问题没有答案。
因此可以改为,可以感知到任务不存在的解决中
代码
复制 -
运行
package net.jcip.examples; import java.util.concurrent.atomic.*; /** * PuzzleSolver * <p/> * Solver that recognizes when no solution exists * * @author Brian Goetz and Tim Peierls */ public class PuzzleSolver <P,M> extends ConcurrentPuzzleSolver<P, M> { PuzzleSolver(Puzzle<P, M> puzzle) { super(puzzle); } private final AtomicInteger taskCount = new AtomicInteger(0); protected Runnable newTask(P p, M m, PuzzleNode<P, M> n) { return new CountingSolverTask(p, m, n); } class CountingSolverTask extends SolverTask { CountingSolverTask(P pos, M move, PuzzleNode<P, M> prev) { super(pos, move, prev); taskCount.incrementAndGet(); } public void run() { try { super.run(); } finally { if (taskCount.decrementAndGet() == 0) solution.setValue(null); } } } }
相关文章推荐
- Java 并发编程实战学习笔记——串行任务转并行任务
- Java 并发编程实战学习笔记——CountDownLatch的使用
- Java 并发编程实战学习笔记
- C++并发编程实战chapter1你好,C++的并发世界--笔记1--任务并行和数据并行
- Java并发编程学习——《Java Concurrency in Practice》学习笔记 6.任务执行
- Java 并发编程实战学习笔记——寻找可强化的并行性
- 【Java并发编程的艺术】【学习笔记】Java内存模型(JMM)
- JAVA并发编程学习笔记之MCS队列锁
- JAVA并发编程学习笔记之synchronized
- 并发编程实战学习笔记(十一)-原子变量与非阻塞同步机制
- JAVA并发编程学习笔记之CLH队列锁
- 并发编程实战学习笔记(六)——线程池的使用
- JAVA并发编程学习笔记之AQS简介
- 学习java并发编程实战的一些心得体会(一)
- java并发编程实战学习(3)--基础构建模块
- JAVA并发编程学习笔记之AQS简介 (r)
- java 并发编程实战书籍学习 第四章 unmodifiableMap
- 学习java并发实战编程(一)线程安全性
- 学习笔记:java并发编程学习之初识Concurrent
- Java并发编程学习笔记 深入理解volatile关键字的作用