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.5 by dl, Mon Feb 19 00:46:06 2007 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/licenses/publicdomain
5   */
6  
7   import java.util.*;
# Line 19 | Line 12 | import java.util.concurrent.atomic.*;
12   public class ConcurrentQueueLoops {
13      static final ExecutorService pool = Executors.newCachedThreadPool();
14      static boolean print = false;
15 <    static final Integer even = new Integer(42);
16 <    static final Integer odd = new Integer(17);
15 >    static final Integer zero = new Integer(0);
16 >    static final Integer one = new Integer(1);
17      static int workMask;
18      static final long RUN_TIME_NANOS = 5 * 1000L * 1000L * 1000L;
19 +    static final int BATCH_SIZE = 8;
20  
21      public static void main(String[] args) throws Exception {
22 <        int maxStages = 48;
23 <        int work = 32;
22 >        int maxStages = 100;
23 >        int work = 1024;
24          Class klass = null;
25          if (args.length > 0) {
26              try {
# Line 49 | Line 43 | public class ConcurrentQueueLoops {
43  
44          print = false;
45          System.out.println("Warmup...");
46 <        oneRun(klass, 4);
46 >        //        oneRun(klass, 4);
47 >        //
48          Thread.sleep(100);
49          oneRun(klass, 1);
50          Thread.sleep(100);
# Line 71 | Line 66 | public class ConcurrentQueueLoops {
66      static final class Stage implements Callable<Integer> {
67          final Queue<Integer> queue;
68          final CyclicBarrier barrier;
69 <        Stage (Queue<Integer> q, CyclicBarrier b) {
69 >        final int nthreads;
70 >        Stage (Queue<Integer> q, CyclicBarrier b, int nthreads) {
71              queue = q;
72              barrier = b;
73 <        }
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;
73 >            this.nthreads = nthreads;
74          }
75  
76          static int compute(int l) {
77              if (l == 0)
78                  return (int)System.nanoTime();
79 <            int nn =  l & workMask;
79 >            int nn =  (l >>> 7) & workMask;
80              while (nn-- > 0)
81 <                l = compute127(l);
81 >                l = LoopHelpers.compute6(l);
82              return l;
83          }
84  
# Line 103 | Line 90 | public class ConcurrentQueueLoops {
90                  int l = (int)now;
91                  int takes = 0;
92                  int misses = 0;
93 +                int lmask = 1;
94                  for (;;) {
95                      l = compute(l);
96                      Integer item = queue.poll();
97                      if (item != null) {
110                        l += item.intValue();
98                          ++takes;
99 +                        if (item == one)
100 +                            l = LoopHelpers.compute6(l);
101                      } else if ((misses++ & 255) == 0 &&
102                                 System.nanoTime() >= stopTime) {
103 <                        break;
103 >                        return new Integer(takes);
104                      } else {
105 <                        Integer a = ((l++ & 4)== 0)? even : odd;
106 <                        queue.add(a);
105 >                        for (int i = 0; i < BATCH_SIZE; ++i) {
106 >                            queue.offer(((l & lmask)== 0)? zero : one);
107 >                            if ((lmask <<= 1) == 0) lmask = 1;
108 >                            if (i != 0) l = compute(l);
109 >                        }
110                      }
111                  }
120                return new Integer(takes);
112              }
113              catch (Exception ie) {
114                  ie.printStackTrace();
# Line 132 | Line 123 | public class ConcurrentQueueLoops {
123          CyclicBarrier barrier = new CyclicBarrier(n + 1, timer);
124          ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>(n);
125          for (int i = 0; i < n; ++i)
126 <            results.add(pool.submit(new Stage(q, barrier)));
126 >            results.add(pool.submit(new Stage(q, barrier, n)));
127  
128          if (print)
129              System.out.print("Threads: " + n + "\t:");
# Line 145 | Line 136 | public class ConcurrentQueueLoops {
136          }
137          long endTime = System.nanoTime();
138          long time = endTime - timer.startTime;
139 <        long tpi = time / total;
149 <        if (print)
150 <            System.out.println(LoopHelpers.rightJustify(tpi) + " ns per item");
139 >        long ips = 1000000000L * total / time;
140          
141 +        if (print)
142 +            System.out.print(LoopHelpers.rightJustify(ips) + " items per sec");
143 +        if (print)
144 +            System.out.println();
145      }
146  
147   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines