Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/test/java/util/concurrent/locks/ReentrantLock/LockOncePerThreadLoops.java
+++ new/test/java/util/concurrent/locks/ReentrantLock/LockOncePerThreadLoops.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
↓ open down ↓ |
26 lines elided |
↑ open up ↑ |
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 * @test
36 36 * @bug 4486658
37 - * @compile LockOncePerThreadLoops.java
37 + * @compile -source 1.5 LockOncePerThreadLoops.java
38 38 * @run main/timeout=15000 LockOncePerThreadLoops
39 39 * @summary Checks for missed signals by locking and unlocking each of an array of locks once per thread
40 40 */
41 41
42 42 import java.util.concurrent.*;
43 43 import java.util.concurrent.locks.*;
44 44 import java.util.*;
45 45
46 46 public final class LockOncePerThreadLoops {
47 47 static final ExecutorService pool = Executors.newCachedThreadPool();
48 48 static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
49 49 static boolean print = false;
50 50 static int nlocks = 50000;
51 51 static int nthreads = 100;
52 52 static int replications = 5;
53 53
54 54 public static void main(String[] args) throws Exception {
55 55 if (args.length > 0)
56 56 replications = Integer.parseInt(args[0]);
57 57
58 58 if (args.length > 1)
59 59 nlocks = Integer.parseInt(args[1]);
60 60
61 61 print = true;
62 62
63 63 for (int i = 0; i < replications; ++i) {
64 64 System.out.print("Iteration: " + i);
65 65 new ReentrantLockLoop().test();
66 66 Thread.sleep(100);
67 67 }
68 68 pool.shutdown();
69 69 if (! pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS))
70 70 throw new Error();
71 71 }
72 72
73 73 static final class ReentrantLockLoop implements Runnable {
74 74 private int v = rng.next();
75 75 private volatile int result = 17;
76 76 final ReentrantLock[]locks = new ReentrantLock[nlocks];
77 77
78 78 private final ReentrantLock lock = new ReentrantLock();
79 79 private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
80 80 private final CyclicBarrier barrier;
81 81 ReentrantLockLoop() {
82 82 barrier = new CyclicBarrier(nthreads+1, timer);
83 83 for (int i = 0; i < nlocks; ++i)
84 84 locks[i] = new ReentrantLock();
85 85 }
86 86
87 87 final void test() throws Exception {
88 88 for (int i = 0; i < nthreads; ++i)
89 89 pool.execute(this);
90 90 barrier.await();
91 91 barrier.await();
92 92 if (print) {
93 93 long time = timer.getTime();
94 94 double secs = (double)(time) / 1000000000.0;
95 95 System.out.println("\t " + secs + "s run time");
96 96 }
97 97
98 98 int r = result;
99 99 if (r == 0) // avoid overoptimization
100 100 System.out.println("useless result: " + r);
101 101 }
102 102
103 103 public final void run() {
104 104 try {
105 105 barrier.await();
106 106 int sum = v;
107 107 int x = 0;
108 108 for (int i = 0; i < locks.length; ++i) {
109 109 locks[i].lock();
110 110 try {
111 111 v = x += ~(v - i);
112 112 }
113 113 finally {
114 114 locks[i].unlock();
115 115 }
116 116 // Once in a while, do something more expensive
117 117 if ((~i & 255) == 0) {
118 118 sum += LoopHelpers.compute1(LoopHelpers.compute2(x));
119 119 }
120 120 else
121 121 sum += sum ^ x;
122 122 }
123 123 barrier.await();
124 124 result += sum;
125 125 }
126 126 catch (Exception ie) {
127 127 return;
128 128 }
129 129 }
130 130 }
131 131
132 132 }
↓ open down ↓ |
85 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX