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.4 by dl, Mon Nov 28 15:40:56 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 49 | Line 50 | public class ConcurrentQueueLoops {
50  
51          print = false;
52          System.out.println("Warmup...");
53 <        oneRun(klass, 4);
53 >        //        oneRun(klass, 4);
54 >        //
55          Thread.sleep(100);
56          oneRun(klass, 1);
57          Thread.sleep(100);
# Line 71 | Line 73 | public class ConcurrentQueueLoops {
73      static final class Stage implements Callable<Integer> {
74          final Queue<Integer> queue;
75          final CyclicBarrier barrier;
76 <        Stage (Queue<Integer> q, CyclicBarrier b) {
76 >        final int nthreads;
77 >        Stage (Queue<Integer> q, CyclicBarrier b, int nthreads) {
78              queue = q;
79              barrier = b;
80 <        }
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;
80 >            this.nthreads = nthreads;
81          }
82  
83          static int compute(int l) {
84              if (l == 0)
85                  return (int)System.nanoTime();
86 <            int nn =  l & workMask;
86 >            int nn =  (l >>> 7) & workMask;
87              while (nn-- > 0)
88 <                l = compute127(l);
88 >                l = LoopHelpers.compute6(l);
89              return l;
90          }
91  
# Line 103 | Line 97 | public class ConcurrentQueueLoops {
97                  int l = (int)now;
98                  int takes = 0;
99                  int misses = 0;
100 +                int lmask = 1;
101                  for (;;) {
102                      l = compute(l);
103                      Integer item = queue.poll();
104                      if (item != null) {
110                        l += item.intValue();
105                          ++takes;
106 +                        if (item == one)
107 +                            l = LoopHelpers.compute6(l);
108                      } else if ((misses++ & 255) == 0 &&
109                                 System.nanoTime() >= stopTime) {
110 <                        break;
110 >                        return new Integer(takes);
111                      } else {
112 <                        Integer a = ((l++ & 4)== 0)? even : odd;
113 <                        queue.add(a);
112 >                        for (int i = 0; i < BATCH_SIZE; ++i) {
113 >                            queue.offer(((l & lmask)== 0)? zero : one);
114 >                            if ((lmask <<= 1) == 0) lmask = 1;
115 >                            if (i != 0) l = compute(l);
116 >                        }
117                      }
118                  }
120                return new Integer(takes);
119              }
120              catch (Exception ie) {
121                  ie.printStackTrace();
# Line 132 | Line 130 | public class ConcurrentQueueLoops {
130          CyclicBarrier barrier = new CyclicBarrier(n + 1, timer);
131          ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>(n);
132          for (int i = 0; i < n; ++i)
133 <            results.add(pool.submit(new Stage(q, barrier)));
133 >            results.add(pool.submit(new Stage(q, barrier, n)));
134  
135          if (print)
136              System.out.print("Threads: " + n + "\t:");
# Line 145 | Line 143 | public class ConcurrentQueueLoops {
143          }
144          long endTime = System.nanoTime();
145          long time = endTime - timer.startTime;
146 <        long tpi = time / total;
149 <        if (print)
150 <            System.out.println(LoopHelpers.rightJustify(tpi) + " ns per item");
146 >        long ips = 1000000000L * total / time;
147          
148 +        if (print)
149 +            System.out.print(LoopHelpers.rightJustify(ips) + " items per sec");
150 +        if (print)
151 +            System.out.println();
152      }
153  
154   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines