Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/test/java/util/concurrent/ConcurrentQueues/LoopHelpers.java
+++ new/test/java/util/concurrent/ConcurrentQueues/LoopHelpers.java
1 1 /*
2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 3 *
4 4 * This code is free software; you can redistribute it and/or modify it
5 5 * under the terms of the GNU General Public License version 2 only, as
6 6 * published by the Free Software Foundation.
7 7 *
8 8 * This code is distributed in the hope that it will be useful, but WITHOUT
9 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 11 * version 2 for more details (a copy is included in the LICENSE file that
12 12 * accompanied this code).
13 13 *
14 14 * You should have received a copy of the GNU General Public License version
15 15 * 2 along with this work; if not, write to the Free Software Foundation,
16 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17 17 *
18 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19 19 * or visit www.oracle.com if you need additional information or have any
20 20 * questions.
21 21 */
22 22
23 23 /*
24 24 * This file is available under and governed by the GNU General Public
25 25 * License version 2 only, as published by the Free Software Foundation.
26 26 * However, the following notice accompanied the original version of this
27 27 * file:
28 28 *
29 29 * Written by Doug Lea with assistance from members of JCP JSR-166
30 30 * Expert Group and released to the public domain, as explained at
31 31 * http://creativecommons.org/licenses/publicdomain
32 32 */
33 33
34 34 /**
35 35 * Misc utilities in JSR166 performance tests
36 36 */
37 37
38 38 import java.util.concurrent.*;
39 39 import java.util.concurrent.atomic.*;
40 40
41 41 class LoopHelpers {
42 42
43 43 // Some mindless computation to do between synchronizations...
44 44
45 45 /**
46 46 * generates 32 bit pseudo-random numbers.
47 47 * Adapted from http://www.snippets.org
48 48 */
49 49 public static int compute1(int x) {
50 50 int lo = 16807 * (x & 0xFFFF);
51 51 int hi = 16807 * (x >>> 16);
52 52 lo += (hi & 0x7FFF) << 16;
53 53 if ((lo & 0x80000000) != 0) {
54 54 lo &= 0x7fffffff;
55 55 ++lo;
56 56 }
57 57 lo += hi >>> 15;
58 58 if (lo == 0 || (lo & 0x80000000) != 0) {
59 59 lo &= 0x7fffffff;
60 60 ++lo;
61 61 }
62 62 return lo;
63 63 }
64 64
65 65 /**
66 66 * Computes a linear congruential random number a random number
67 67 * of times.
68 68 */
69 69 public static int compute2(int x) {
70 70 int loops = (x >>> 4) & 7;
71 71 while (loops-- > 0) {
↓ open down ↓ |
71 lines elided |
↑ open up ↑ |
72 72 x = (x * 2147483647) % 16807;
73 73 }
74 74 return x;
75 75 }
76 76
77 77 /**
78 78 * An actually useful random number generator, but unsynchronized.
79 79 * Basically same as java.util.Random.
80 80 */
81 81 public static class SimpleRandom {
82 - private final static long multiplier = 0x5DEECE66DL;
83 - private final static long addend = 0xBL;
84 - private final static long mask = (1L << 48) - 1;
82 + private static final long multiplier = 0x5DEECE66DL;
83 + private static final long addend = 0xBL;
84 + private static final long mask = (1L << 48) - 1;
85 85 static final AtomicLong seq = new AtomicLong(1);
86 86 private long seed = System.nanoTime() + seq.getAndIncrement();
87 87
88 88 public void setSeed(long s) {
89 89 seed = s;
90 90 }
91 91
92 92 public int next() {
93 93 long nextseed = (seed * multiplier + addend) & mask;
94 94 seed = nextseed;
95 95 return ((int)(nextseed >>> 17)) & 0x7FFFFFFF;
96 96 }
97 97 }
98 98
99 99 public static class BarrierTimer implements Runnable {
100 100 public volatile long startTime;
101 101 public volatile long endTime;
102 102 public void run() {
103 103 long t = System.nanoTime();
104 104 if (startTime == 0)
105 105 startTime = t;
106 106 else
107 107 endTime = t;
108 108 }
109 109 public void clear() {
110 110 startTime = 0;
111 111 endTime = 0;
112 112 }
113 113 public long getTime() {
114 114 return endTime - startTime;
115 115 }
116 116 }
117 117
118 118 public static String rightJustify(long n) {
119 119 // There's probably a better way to do this...
120 120 String field = " ";
121 121 String num = Long.toString(n);
122 122 if (num.length() >= field.length())
123 123 return num;
124 124 StringBuffer b = new StringBuffer(field);
125 125 b.replace(b.length()-num.length(), b.length(), num);
126 126 return b.toString();
127 127 }
128 128
129 129 }
↓ open down ↓ |
35 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX