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.3 by dl, Fri Oct 7 14:58:09 2005 UTC

# Line 19 | Line 19 | import java.util.concurrent.atomic.*;
19   public class ConcurrentQueueLoops {
20      static final ExecutorService pool = Executors.newCachedThreadPool();
21      static boolean print = false;
22 <    static final Integer even = new Integer(42);
23 <    static final Integer odd = new Integer(17);
22 >    static final Integer zero = new Integer(0);
23 >    static final Integer one = new Integer(1);
24      static int workMask;
25      static final long RUN_TIME_NANOS = 5 * 1000L * 1000L * 1000L;
26 +    static final int BATCH_SIZE = 8;
27  
28      public static void main(String[] args) throws Exception {
29 <        int maxStages = 48;
30 <        int work = 32;
29 >        int maxStages = 100;
30 >        int work = 1024;
31          Class klass = null;
32          if (args.length > 0) {
33              try {
# Line 71 | Line 72 | public class ConcurrentQueueLoops {
72      static final class Stage implements Callable<Integer> {
73          final Queue<Integer> queue;
74          final CyclicBarrier barrier;
75 <        Stage (Queue<Integer> q, CyclicBarrier b) {
75 >        final int nthreads;
76 >        Stage (Queue<Integer> q, CyclicBarrier b, int nthreads) {
77              queue = q;
78              barrier = b;
79 <        }
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;
79 >            this.nthreads = nthreads;
80          }
81  
82          static int compute(int l) {
83              if (l == 0)
84                  return (int)System.nanoTime();
85 <            int nn =  l & workMask;
85 >            int nn =  (l >>> 7) & workMask;
86              while (nn-- > 0)
87 <                l = compute127(l);
87 >                l = LoopHelpers.compute6(l);
88              return l;
89          }
90  
# Line 103 | Line 96 | public class ConcurrentQueueLoops {
96                  int l = (int)now;
97                  int takes = 0;
98                  int misses = 0;
99 +                int lmask = 1;
100                  for (;;) {
101                      l = compute(l);
102                      Integer item = queue.poll();
103                      if (item != null) {
110                        l += item.intValue();
104                          ++takes;
105 +                        if (item == one)
106 +                            l = LoopHelpers.compute6(l);
107                      } else if ((misses++ & 255) == 0 &&
108                                 System.nanoTime() >= stopTime) {
109 <                        break;
109 >                        return new Integer(takes);
110                      } else {
111 <                        Integer a = ((l++ & 4)== 0)? even : odd;
112 <                        queue.add(a);
111 >                        for (int i = 0; i < BATCH_SIZE; ++i) {
112 >                            queue.offer(((l & lmask)== 0)? zero : one);
113 >                            if ((lmask <<= 1) == 0) lmask = 1;
114 >                            if (i != 0) l = compute(l);
115 >                        }
116                      }
117                  }
120                return new Integer(takes);
118              }
119              catch (Exception ie) {
120                  ie.printStackTrace();
# Line 132 | Line 129 | public class ConcurrentQueueLoops {
129          CyclicBarrier barrier = new CyclicBarrier(n + 1, timer);
130          ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>(n);
131          for (int i = 0; i < n; ++i)
132 <            results.add(pool.submit(new Stage(q, barrier)));
132 >            results.add(pool.submit(new Stage(q, barrier, n)));
133  
134          if (print)
135              System.out.print("Threads: " + n + "\t:");
# Line 145 | Line 142 | public class ConcurrentQueueLoops {
142          }
143          long endTime = System.nanoTime();
144          long time = endTime - timer.startTime;
145 <        long tpi = time / total;
149 <        if (print)
150 <            System.out.println(LoopHelpers.rightJustify(tpi) + " ns per item");
145 >        long ips = 1000000000L * total / time;
146          
147 +        if (print)
148 +            System.out.print(LoopHelpers.rightJustify(ips) + " items per sec");
149 +        if (print)
150 +            System.out.println();
151      }
152  
153   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines