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.3 by dl, Fri Oct 7 14:58:09 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();
# Line 28 | Line 24 | public class ConcurrentQueueLoops {
24      public static void main(String[] args) throws Exception {
25          int maxStages = 100;
26          int work = 1024;
27 <        Class klass = null;
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 50 | 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;
60 <        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          final int nthreads;
72 <        Stage (Queue<Integer> q, CyclicBarrier b, int nthreads) {
73 <            queue = q;
72 >        Stage(Queue<Integer> q, CyclicBarrier b, int nthreads) {
73 >            queue = q;
74              barrier = b;
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 >>> 7) & workMask;
82 <            while (nn-- > 0)
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          }
# Line 93 | 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;
# Line 106 | Line 102 | public class ConcurrentQueueLoops {
102                              l = LoopHelpers.compute6(l);
103                      } else if ((misses++ & 255) == 0 &&
104                                 System.nanoTime() >= stopTime) {
105 <                        return new Integer(takes);
105 >                        return Integer.valueOf(takes);
106                      } else {
107                          for (int i = 0; i < BATCH_SIZE; ++i) {
108 <                            queue.offer(((l & lmask)== 0)? zero : one);
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                  }
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)
128 >        for (int i = 0; i < n; ++i)
129              results.add(pool.submit(new Stage(q, barrier, n)));
130  
131          if (print)
# Line 143 | Line 140 | public class ConcurrentQueueLoops {
140          long endTime = System.nanoTime();
141          long time = endTime - timer.startTime;
142          long ips = 1000000000L * total / time;
143 <        
144 <        if (print)
143 >
144 >        if (print)
145              System.out.print(LoopHelpers.rightJustify(ips) + " items per sec");
146          if (print)
147              System.out.println();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines