jdk/test/java/util/concurrent/locks/ReentrantLock/LockOncePerThreadLoops.java
author dl
Thu, 07 Apr 2011 15:06:32 +0100
changeset 9242 ef138d47df58
parent 7518 0282db800fe1
child 22293 9a913cb32aae
permissions -rw-r--r--
7034657: Update Creative Commons license URL in legal notices Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    18
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    19
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4347
diff changeset
    20
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * Expert Group and released to the public domain, as explained at
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 7518
diff changeset
    31
 * http://creativecommons.org/publicdomain/zero/1.0/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @bug 4486658
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    37
 * @compile -source 1.5 LockOncePerThreadLoops.java
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @run main/timeout=15000 LockOncePerThreadLoops
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @summary Checks for missed signals by locking and unlocking each of an array of locks once per thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.concurrent.locks.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public final class LockOncePerThreadLoops {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static final ExecutorService pool = Executors.newCachedThreadPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static boolean print = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static int nlocks = 50000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static int nthreads = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static int replications = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if (args.length > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            replications = Integer.parseInt(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (args.length > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            nlocks = Integer.parseInt(args[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        print = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        for (int i = 0; i < replications; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            System.out.print("Iteration: " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            new ReentrantLockLoop().test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        pool.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if (! pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    static final class ReentrantLockLoop implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        private int v = rng.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        private volatile int result = 17;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        final ReentrantLock[]locks = new ReentrantLock[nlocks];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        private final ReentrantLock lock = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        private final CyclicBarrier barrier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        ReentrantLockLoop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            barrier = new CyclicBarrier(nthreads+1, timer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            for (int i = 0; i < nlocks; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                locks[i] = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        final void test() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            for (int i = 0; i < nthreads; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                pool.execute(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            if (print) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                long time = timer.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                double secs = (double)(time) / 1000000000.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                System.out.println("\t " + secs + "s run time");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            int r = result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            if (r == 0) // avoid overoptimization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                System.out.println("useless result: " + r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        public final void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                int sum = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                int x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                for (int i = 0; i < locks.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    locks[i].lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                            v = x += ~(v - i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        locks[i].unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    // Once in a while, do something more expensive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    if ((~i & 255) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        sum += LoopHelpers.compute1(LoopHelpers.compute2(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        sum += sum ^ x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                result += sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            catch (Exception ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
}