Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add concurrent time test

  • Loading branch information...
commit a388e4fa00bf86c094dc5d1d938ac02bbdffe8e5 1 parent 3b5f369
Feng Shen authored February 20, 2011
109  src/test/java/jcip/puzzle/ConcurrentQueenPuzzleSolver.java
... ...
@@ -0,0 +1,109 @@
  1
+package jcip.puzzle;
  2
+
  3
+import java.util.List;
  4
+import java.util.Random;
  5
+import java.util.concurrent.CountDownLatch;
  6
+import java.util.concurrent.ExecutorService;
  7
+import java.util.concurrent.Executors;
  8
+import java.util.concurrent.ThreadPoolExecutor;
  9
+
  10
+import javax.annotation.concurrent.GuardedBy;
  11
+
  12
+public class ConcurrentQueenPuzzleSolver {
  13
+
  14
+    public static void main(String[] args) throws InterruptedException {
  15
+        for (int i = 8; i < 100; ++i) {
  16
+            long start = System.currentTimeMillis();
  17
+            QueenPuzzle puzzle = new QueenPuzzle(i);
  18
+            ConcurrentQueenPuzzleSolver solver = new ConcurrentQueenPuzzleSolver(
  19
+                    puzzle);
  20
+            List<Move> result = solver.solve();
  21
+            System.out.println("solve problem size of " + i + " takes time "
  22
+                    + (System.currentTimeMillis() - start) + " ms; result: "
  23
+                    + result);
  24
+        }
  25
+    }
  26
+
  27
+    final ValueLatch<Node<Position, Move>> solution = new ValueLatch<Node<Position, Move>>();
  28
+    private final QueenPuzzle puzzle;
  29
+
  30
+    private final ExecutorService exec = Executors.newFixedThreadPool(50);
  31
+
  32
+    public List<Move> solve() throws InterruptedException {
  33
+        try {
  34
+            Position pos = puzzle.initialPosition();
  35
+            exec.execute(newTask(pos, null, null));
  36
+            // block until solution is found
  37
+            Node<Position, Move> result = solution.getValue();
  38
+            return result.asMoveList();
  39
+        } finally {
  40
+            exec.shutdown();
  41
+        }
  42
+    }
  43
+
  44
+    protected Runnable newTask(Position p, Move m, Node<Position, Move> prev) {
  45
+        return new SolverTask(p, m, prev);
  46
+    }
  47
+
  48
+    public ConcurrentQueenPuzzleSolver(QueenPuzzle puzzle) {
  49
+        this.puzzle = puzzle;
  50
+        ((ThreadPoolExecutor) exec)
  51
+                .setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
  52
+
  53
+    }
  54
+
  55
+    class SolverTask extends Node<Position, Move> implements Runnable {
  56
+
  57
+        public SolverTask(Position pos, Move move, Node<Position, Move> prev) {
  58
+            super(pos, move, prev);
  59
+        }
  60
+
  61
+        @Override
  62
+        public void run() {
  63
+            if (solution.isSet())
  64
+                return;
  65
+            if (puzzle.isGoal(pos)) {
  66
+                solution.setValue(this);
  67
+            } else {
  68
+                for (Move move : puzzle.legalMoves(pos)) {
  69
+                    Position newPos = puzzle.move(pos, move);
  70
+                    if (puzzle.isGoal(newPos)) {
  71
+                        solution.setValue(this);
  72
+                        return;
  73
+                    } else {
  74
+                        for (Move m : puzzle.legalMoves(newPos)) {
  75
+                            exec.execute(newTask(puzzle.move(newPos, m), m,
  76
+                                    this));
  77
+                        }
  78
+                    }
  79
+                }
  80
+            }
  81
+        }
  82
+    }
  83
+
  84
+    static class ValueLatch<T> {
  85
+
  86
+        @GuardedBy("this")
  87
+        private final CountDownLatch done = new CountDownLatch(1);
  88
+        private T value;
  89
+
  90
+        public boolean isSet() {
  91
+            return done.getCount() == 0;
  92
+        }
  93
+
  94
+        public synchronized void setValue(T newValue) {
  95
+            if (!isSet()) {
  96
+                value = newValue;
  97
+                done.countDown();
  98
+            }
  99
+        }
  100
+
  101
+        public T getValue() throws InterruptedException {
  102
+            done.await();
  103
+            synchronized (this) {
  104
+                return value;
  105
+            }
  106
+        }
  107
+    }
  108
+
  109
+}
82  src/test/java/jcip/puzzle/PQueenPuzzleSolver.java
... ...
@@ -0,0 +1,82 @@
  1
+package jcip.puzzle;
  2
+
  3
+import java.util.List;
  4
+import java.util.concurrent.Callable;
  5
+import java.util.concurrent.CompletionService;
  6
+import java.util.concurrent.CountDownLatch;
  7
+import java.util.concurrent.ExecutionException;
  8
+import java.util.concurrent.ExecutorCompletionService;
  9
+import java.util.concurrent.ExecutorService;
  10
+import java.util.concurrent.Executors;
  11
+import java.util.concurrent.Future;
  12
+
  13
+public class PQueenPuzzleSolver {
  14
+
  15
+    private final Puzzle<Position, Move> puzzle;
  16
+    final CountDownLatch latch = new CountDownLatch(1);
  17
+
  18
+    public PQueenPuzzleSolver(QueenPuzzle puzzle) {
  19
+        this.puzzle = puzzle;
  20
+    }
  21
+
  22
+    public List<Move> solve() throws InterruptedException, ExecutionException {
  23
+        final ExecutorService exec = Executors.newFixedThreadPool(Runtime
  24
+                .getRuntime().availableProcessors());
  25
+        try {
  26
+            final Position pos = puzzle.initialPosition();
  27
+            final Node<Position, Move> init = new Node<Position, Move>(pos,
  28
+                    null, null);
  29
+            CompletionService<List<Move>> completionService = new ExecutorCompletionService<List<Move>>(
  30
+                    exec);
  31
+            for (final Move m : puzzle.legalMoves(pos)) {
  32
+                completionService.submit(new Callable<List<Move>>() {
  33
+                    @Override
  34
+                    public List<Move> call() throws Exception {
  35
+                        Position newPos = puzzle.move(pos, m);
  36
+                        return search(new Node<Position, Move>(newPos, m, init));
  37
+                    }
  38
+                });
  39
+            }
  40
+            while (true) {
  41
+                Future<List<Move>> f = completionService.take();
  42
+                List<Move> result = f.get();
  43
+                if (result != null)
  44
+                    return result;
  45
+            }
  46
+        } finally {
  47
+            exec.shutdownNow();
  48
+        }
  49
+    }
  50
+
  51
+    private List<Move> search(Node<Position, Move> node) {
  52
+        if (latch.getCount() == 0)
  53
+            return null;
  54
+
  55
+        if (puzzle.isGoal(node.pos)) {
  56
+            latch.countDown();
  57
+            return node.asMoveList();
  58
+        }
  59
+        for (Move move : puzzle.legalMoves(node.pos)) {
  60
+            Position pos = puzzle.move(node.pos, move);
  61
+            Node<Position, Move> child = new Node<Position, Move>(pos, move,
  62
+                    node);
  63
+            List<Move> result = search(child);
  64
+            if (result != null)
  65
+                return result;
  66
+        }
  67
+        return null;
  68
+    }
  69
+
  70
+    public static void main(String[] args) throws InterruptedException,
  71
+            ExecutionException {
  72
+        for (int i = 8; i < 100; ++i) {
  73
+            long start = System.currentTimeMillis();
  74
+            QueenPuzzle puzzle = new QueenPuzzle(i);
  75
+            PQueenPuzzleSolver solver = new PQueenPuzzleSolver(puzzle);
  76
+            List<Move> result = solver.solve();
  77
+            System.out.println("solve problem size of " + i + " takes time "
  78
+                    + (System.currentTimeMillis() - start) + " ms; result: "
  79
+                    + result);
  80
+        }
  81
+    }
  82
+}
4  src/test/java/jcip/puzzle/QueenPuzzleSolver.java
@@ -32,9 +32,9 @@ public QueenPuzzleSolver(QueenPuzzle puzzle) {
32 32
     }
33 33
 
34 34
     public static void main(String[] args) {
35  
-        for (int i = 8; i < 100; ++i) {
  35
+        for (int i = 0; i < 30; ++i) {
36 36
             long start = System.currentTimeMillis();
37  
-            QueenPuzzle puzzle = new QueenPuzzle(i);
  37
+            QueenPuzzle puzzle = new QueenPuzzle(29);
38 38
             QueenPuzzleSolver solver = new QueenPuzzleSolver(puzzle);
39 39
             List<Move> result = solver.solve();
40 40
             System.out.println("solve problem size of " + i + " takes time "
70  src/test/java/jcip/puzzle/TestConcurrenTime.java
... ...
@@ -0,0 +1,70 @@
  1
+package jcip.puzzle;
  2
+
  3
+import java.util.List;
  4
+import java.util.concurrent.ExecutorService;
  5
+import java.util.concurrent.Executors;
  6
+import java.util.concurrent.TimeUnit;
  7
+import java.util.concurrent.atomic.AtomicLong;
  8
+
  9
+import org.junit.Test;
  10
+
  11
+public class TestConcurrenTime {
  12
+
  13
+    private long f(int n) {
  14
+        if (n < 3)
  15
+            return 1;
  16
+        else
  17
+            return f(n - 1) + f(n - 2);
  18
+    }
  19
+
  20
+    @Test
  21
+    public void testF() {
  22
+        for (int i = 0; i < 3; ++i) {
  23
+            long l = System.currentTimeMillis();
  24
+            // System.out.println(f(44));
  25
+            QueenPuzzle puzzle = new QueenPuzzle(29);
  26
+            QueenPuzzleSolver solver = new QueenPuzzleSolver(puzzle);
  27
+            List<Move> result = solver.solve();
  28
+            System.out.println(System.currentTimeMillis() - l);
  29
+        }
  30
+    }
  31
+
  32
+    private static class Task implements Runnable {
  33
+
  34
+        private AtomicLong time;
  35
+
  36
+        public Task(AtomicLong time) {
  37
+            this.time = time;
  38
+        }
  39
+
  40
+        @Override
  41
+        public void run() {
  42
+            long start = System.currentTimeMillis();
  43
+            QueenPuzzle puzzle = new QueenPuzzle(29);
  44
+            QueenPuzzleSolver solver = new QueenPuzzleSolver(puzzle);
  45
+            List<Move> result = solver.solve();
  46
+            long t = System.currentTimeMillis() - start;
  47
+            time.addAndGet(t);
  48
+        }
  49
+
  50
+    }
  51
+
  52
+    @Test
  53
+    public void testConcurrentTime() throws InterruptedException {
  54
+        for (int j = 24; j > 0; j--) {
  55
+            ExecutorService exec = Executors.newFixedThreadPool(j);
  56
+            final AtomicLong time = new AtomicLong(0);
  57
+
  58
+            long start = System.currentTimeMillis();
  59
+            for (int i = 0; i < 60; ++i) {
  60
+                exec.submit(new Task(time));
  61
+            }
  62
+            exec.shutdown();
  63
+            exec.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
  64
+            long htime = System.currentTimeMillis() - start;
  65
+            System.out.println(j + " thread total: " + time.get() + " htime "
  66
+                    + htime);
  67
+        }
  68
+    }
  69
+
  70
+}

0 notes on commit a388e4f

Please sign in to comment.
Something went wrong with that request. Please try again.