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

Comparing jsr166/src/test/loops/ConcurrentQueueLoops.java (file contents):
Revision 1.2 by dl, Tue Oct 4 20:09:41 2005 UTC vs.
Revision 1.19 by jsr166, Sat Dec 31 19:02:43 2016 UTC

# Line 1 | Line 1
1   /*
2 * @test %I% %E%
3 * @bug 4486658
4 * @compile -source 1.5 ConcurrentQueueLoops.java
5 * @run main/timeout=230 ConcurrentQueueLoops
6 * @summary Checks that a set of threads can repeatedly get and modify items
7 */
8 /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3 < * Expert Group and released to the public domain. Use, modify, and
4 < * redistribute this code in any way without acknowledgement.
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 < import java.util.*;
8 < import java.util.concurrent.*;
9 < import java.util.concurrent.locks.*;
10 < import java.util.concurrent.atomic.*;
7 > import java.util.ArrayList;
8 > import java.util.Queue;
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  
15   public class ConcurrentQueueLoops {
16      static final ExecutorService pool = Executors.newCachedThreadPool();
17      static boolean print = false;
18 <    static final Integer even = new Integer(42);
19 <    static final Integer odd = new Integer(17);
18 >    static final Integer zero = new Integer(0);
19 >    static final Integer one = new Integer(1);
20      static int workMask;
21      static final long RUN_TIME_NANOS = 5 * 1000L * 1000L * 1000L;
22 +    static final int BATCH_SIZE = 8;
23  
24      public static void main(String[] args) throws Exception {
25 <        int maxStages = 48;
26 <        int work = 32;
27 <        Class klass = null;
25 >        int maxStages = 100;
26 >        int work = 1024;
27 >        Class<?> klass = null;
28          if (args.length > 0) {
29              try {
30                  klass = Class.forName(args[0]);
31 <            } catch(ClassNotFoundException e) {
31 >            } catch (ClassNotFoundException e) {
32                  throw new RuntimeException("Class " + args[0] + " not found.");
33              }
34          }
35  
36 <        if (args.length > 1)
36 >        if (args.length > 1)
37              maxStages = Integer.parseInt(args[1]);
38  
39 <        if (args.length > 2)
39 >        if (args.length > 2)
40              work = Integer.parseInt(args[2]);
41  
42          workMask = work - 1;
# Line 49 | Line 46 | public class ConcurrentQueueLoops {
46  
47          print = false;
48          System.out.println("Warmup...");
49 <        oneRun(klass, 4);
49 >        //        oneRun(klass, 4);
50 >        //
51          Thread.sleep(100);
52          oneRun(klass, 1);
53          Thread.sleep(100);
54          print = true;
55  
56 <        int k = 1;
59 <        for (int i = 1; i <= maxStages;) {
56 >        for (int k = 1, i = 1; i <= maxStages;) {
57              oneRun(klass, i);
58              if (i == k) {
59                  k = i << 1;
60                  i = i + (i >>> 1);
61 <            }
62 <            else
61 >            }
62 >            else
63                  i = k;
64          }
65          pool.shutdown();
66 <   }
66 >    }
67  
68      static final class Stage implements Callable<Integer> {
69          final Queue<Integer> queue;
70          final CyclicBarrier barrier;
71 <        Stage (Queue<Integer> q, CyclicBarrier b) {
72 <            queue = q;
71 >        final int nthreads;
72 >        Stage(Queue<Integer> q, CyclicBarrier b, int nthreads) {
73 >            queue = q;
74              barrier = b;
75 <        }
78 <
79 <        static int compute127(int l) {
80 <            l = LoopHelpers.compute1(l);
81 <            l = LoopHelpers.compute2(l);
82 <            l = LoopHelpers.compute3(l);
83 <            l = LoopHelpers.compute4(l);
84 <            l = LoopHelpers.compute5(l);
85 <            l = LoopHelpers.compute6(l);
86 <            return l;
75 >            this.nthreads = nthreads;
76          }
77  
78          static int compute(int l) {
79 <            if (l == 0)
80 <                return (int)System.nanoTime();
81 <            int nn =  l & workMask;
82 <            while (nn-- > 0)
83 <                l = compute127(l);
79 >            if (l == 0)
80 >                return (int) System.nanoTime();
81 >            int nn = (l >>> 7) & workMask;
82 >            while (nn-- > 0)
83 >                l = LoopHelpers.compute6(l);
84              return l;
85          }
86  
# Line 100 | Line 89 | public class ConcurrentQueueLoops {
89                  barrier.await();
90                  long now = System.nanoTime();
91                  long stopTime = now + RUN_TIME_NANOS;
92 <                int l = (int)now;
92 >                int l = (int) now;
93                  int takes = 0;
94                  int misses = 0;
95 +                int lmask = 1;
96                  for (;;) {
97                      l = compute(l);
98                      Integer item = queue.poll();
99                      if (item != null) {
110                        l += item.intValue();
100                          ++takes;
101 +                        if (item == one)
102 +                            l = LoopHelpers.compute6(l);
103                      } else if ((misses++ & 255) == 0 &&
104                                 System.nanoTime() >= stopTime) {
105 <                        break;
105 >                        return Integer.valueOf(takes);
106                      } else {
107 <                        Integer a = ((l++ & 4)== 0)? even : odd;
108 <                        queue.add(a);
107 >                        for (int i = 0; i < BATCH_SIZE; ++i) {
108 >                            queue.offer(((l & lmask)== 0) ? zero : one);
109 >                            if ((lmask <<= 1) == 0) lmask = 1;
110 >                            if (i != 0) l = compute(l);
111 >                        }
112                      }
113                  }
120                return new Integer(takes);
114              }
115 <            catch (Exception ie) {
115 >            catch (Exception ie) {
116                  ie.printStackTrace();
117                  throw new Error("Call loop failed");
118              }
119          }
120      }
121  
122 <    static void oneRun(Class klass, int n) throws Exception {
123 <        Queue<Integer> q = (Queue<Integer>)klass.newInstance();
122 >    static void oneRun(Class<?> klass, int n) throws Exception {
123 >        Queue<Integer> q =
124 >            (Queue<Integer>) klass.getConstructor().newInstance();
125          LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
126          CyclicBarrier barrier = new CyclicBarrier(n + 1, timer);
127          ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>(n);
128 <        for (int i = 0; i < n; ++i)
129 <            results.add(pool.submit(new Stage(q, barrier)));
128 >        for (int i = 0; i < n; ++i)
129 >            results.add(pool.submit(new Stage(q, barrier, n)));
130  
131          if (print)
132              System.out.print("Threads: " + n + "\t:");
# Line 145 | Line 139 | public class ConcurrentQueueLoops {
139          }
140          long endTime = System.nanoTime();
141          long time = endTime - timer.startTime;
142 <        long tpi = time / total;
142 >        long ips = 1000000000L * total / time;
143 >
144 >        if (print)
145 >            System.out.print(LoopHelpers.rightJustify(ips) + " items per sec");
146          if (print)
147 <            System.out.println(LoopHelpers.rightJustify(tpi) + " ns per item");
151 <        
147 >            System.out.println();
148      }
149  
150   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines