ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/ConcurrentDequeLoops.java
(Generate patch)

Comparing jsr166/src/test/loops/ConcurrentDequeLoops.java (file contents):
Revision 1.2 by dl, Mon Feb 19 00:46:06 2007 UTC vs.
Revision 1.14 by jsr166, Sat Dec 31 19:02:43 2016 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 < import java.util.*;
8 < import java.util.concurrent.*;
9 < import java.util.concurrent.atomic.*;
10 < import java.util.concurrent.locks.*;
7 > import java.util.ArrayList;
8 > import java.util.Deque;
9 > import java.util.concurrent.Callable;
10 > import java.util.concurrent.CyclicBarrier;
11 > import java.util.concurrent.ExecutorService;
12 > import java.util.concurrent.Executors;
13 > import java.util.concurrent.Future;
14 > import java.util.concurrent.atomic.AtomicInteger;
15  
16   public class ConcurrentDequeLoops {
17      static final ExecutorService pool = Executors.newCachedThreadPool();
# Line 18 | Line 22 | public class ConcurrentDequeLoops {
22          int maxStages = 8;
23          int items = 1000000;
24  
25 <        Class klass = null;
25 >        Class<?> klass = null;
26          if (args.length > 0) {
27              try {
28                  klass = Class.forName(args[0]);
29 <            } catch(ClassNotFoundException e) {
29 >            } catch (ClassNotFoundException e) {
30                  throw new RuntimeException("Class " + args[0] + " not found.");
31              }
32          }
33 <        else
33 >        else
34              throw new Error();
35  
36 <        if (args.length > 1)
36 >        if (args.length > 1)
37              maxStages = Integer.parseInt(args[1]);
38  
39          System.out.print("Class: " + klass.getName());
# Line 43 | Line 47 | public class ConcurrentDequeLoops {
47          Thread.sleep(100);
48          print = true;
49  
50 <        int k = 1;
47 <        for (int i = 1; i <= maxStages;) {
50 >        for (int k = 1, i = 1; i <= maxStages;) {
51              oneRun(klass, i, items);
52              if (i == k) {
53                  k = i << 1;
54                  i = i + (i >>> 1);
55 <            }
56 <            else
55 >            }
56 >            else
57                  i = k;
58          }
59          pool.shutdown();
60 <   }
60 >    }
61  
62      static class Stage implements Callable<Integer> {
63          final Deque<Integer> queue;
64          final CyclicBarrier barrier;
65          final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
66          int items;
67 <        Stage (Deque<Integer> q, CyclicBarrier b, int items) {
68 <            queue = q;
67 >        Stage(Deque<Integer> q, CyclicBarrier b, int items) {
68 >            queue = q;
69              barrier = b;
70              this.items = items;
71          }
# Line 72 | Line 75 | public class ConcurrentDequeLoops {
75              // transform it, and put back in.
76              try {
77                  barrier.await();
78 <                int l = (int)System.nanoTime();
78 >                int l = (int) System.nanoTime();
79                  int takes = 0;
80                  for (;;) {
81                      Integer item;
# Line 100 | Line 103 | public class ConcurrentDequeLoops {
103                          else
104                              queue.addLast(res);
105                      }
106 <                    else { // spinwait
106 >                    else { // spinwait
107                          for (int k = 1 + (l & 15); k != 0; --k)
108                              l = LoopHelpers.compute1(LoopHelpers.compute2(l));
109                          if ((l & 3) == 3) {
# Line 110 | Line 113 | public class ConcurrentDequeLoops {
113                  }
114                  return new Integer(l);
115              }
116 <            catch (Exception ie) {
116 >            catch (Exception ie) {
117                  ie.printStackTrace();
118                  throw new Error("Call loop failed");
119              }
120          }
121      }
122  
123 <    static void oneRun(Class klass, int n, int items) throws Exception {
124 <        Deque<Integer> q = (Deque<Integer>)klass.newInstance();
123 >    static void oneRun(Class<?> klass, int n, int items) throws Exception {
124 >        Deque<Integer> q =
125 >            (Deque<Integer>) klass.getConstructor().newInstance();
126          LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
127          CyclicBarrier barrier = new CyclicBarrier(n + 1, timer);
128          totalItems = new AtomicInteger(n * items);
129          ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>(n);
130 <        for (int i = 0; i < n; ++i)
130 >        for (int i = 0; i < n; ++i)
131              results.add(pool.submit(new Stage(q, barrier, items)));
132  
133          if (print)
# Line 141 | Line 145 | public class ConcurrentDequeLoops {
145              System.out.println(LoopHelpers.rightJustify(time / (items * n)) + " ns per item");
146          if (total == 0) // avoid overoptimization
147              System.out.println("useless result: " + total);
144        
148      }
149   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines