0N/A/*
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * This file is available under and governed by the GNU General Public
0N/A * License version 2 only, as published by the Free Software Foundation.
0N/A * However, the following notice accompanied the original version of this
0N/A * file:
0N/A *
0N/A * Written by Doug Lea with assistance from members of JCP JSR-166
0N/A * Expert Group and released to the public domain, as explained at
3984N/A * http://creativecommons.org/publicdomain/zero/1.0/
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4486658
3203N/A * @compile -source 1.5 SimpleReentrantLockLoops.java
0N/A * @run main/timeout=4500 SimpleReentrantLockLoops
0N/A * @summary multiple threads using a single lock
0N/A */
0N/A
0N/Aimport java.util.concurrent.*;
0N/Aimport java.util.concurrent.locks.*;
0N/Aimport java.util.*;
0N/A
0N/Apublic final class SimpleReentrantLockLoops {
0N/A static final ExecutorService pool = Executors.newCachedThreadPool();
0N/A static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
0N/A static boolean print = false;
0N/A static int iters = 1000000;
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A int maxThreads = 5;
0N/A if (args.length > 0)
0N/A maxThreads = Integer.parseInt(args[0]);
0N/A
0N/A print = true;
0N/A
0N/A int reps = 2;
0N/A for (int i = 1; i <= maxThreads; i += (i+1) >>> 1) {
0N/A int n = reps;
0N/A if (reps > 1) --reps;
0N/A while (n-- > 0) {
0N/A System.out.print("Threads: " + i);
0N/A new ReentrantLockLoop(i).test();
0N/A Thread.sleep(100);
0N/A }
0N/A }
0N/A pool.shutdown();
0N/A if (! pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS))
0N/A throw new Error();
0N/A }
0N/A
0N/A static final class ReentrantLockLoop implements Runnable {
0N/A private int v = rng.next();
0N/A private volatile int result = 17;
0N/A private final ReentrantLock lock = new ReentrantLock();
0N/A private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
0N/A private final CyclicBarrier barrier;
0N/A private final int nthreads;
0N/A ReentrantLockLoop(int nthreads) {
0N/A this.nthreads = nthreads;
0N/A barrier = new CyclicBarrier(nthreads+1, timer);
0N/A }
0N/A
0N/A final void test() throws Exception {
0N/A for (int i = 0; i < nthreads; ++i)
0N/A pool.execute(this);
0N/A barrier.await();
0N/A barrier.await();
0N/A if (print) {
0N/A long time = timer.getTime();
0N/A long tpi = time / ((long)iters * nthreads);
0N/A System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per lock");
0N/A double secs = (double)(time) / 1000000000.0;
0N/A System.out.println("\t " + secs + "s run time");
0N/A }
0N/A
0N/A int r = result;
0N/A if (r == 0) // avoid overoptimization
0N/A System.out.println("useless result: " + r);
0N/A }
0N/A
0N/A public final void run() {
0N/A try {
0N/A barrier.await();
0N/A int sum = v;
0N/A int x = 0;
0N/A int n = iters;
0N/A do {
0N/A lock.lock();
0N/A try {
0N/A if ((n & 255) == 0)
0N/A v = x = LoopHelpers.compute2(LoopHelpers.compute1(v));
0N/A else
0N/A v = x += ~(v - n);
0N/A }
0N/A finally {
0N/A lock.unlock();
0N/A }
0N/A // Once in a while, do something more expensive
0N/A if ((~n & 255) == 0) {
0N/A sum += LoopHelpers.compute1(LoopHelpers.compute2(x));
0N/A }
0N/A else
0N/A sum += sum ^ x;
0N/A } while (n-- > 0);
0N/A barrier.await();
0N/A result += sum;
0N/A }
0N/A catch (Exception ie) {
0N/A return;
0N/A }
0N/A }
0N/A }
0N/A
0N/A}