jdk/test/java/util/concurrent/locks/ReentrantLock/CancelledLockLoops.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 4347 ab0a9f495844
child 7518 0282db800fe1
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * http://creativecommons.org/licenses/publicdomain
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
4347
ab0a9f495844 6907177: Update jdk tests to remove unncessary -source and -target options
darcy
parents: 4110
diff changeset
    37
 * @compile CancelledLockLoops.java
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @run main/timeout=2800 CancelledLockLoops
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @summary tests lockInterruptibly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Checks for responsiveness of locks to interrupts. Runs under that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * assumption that ITERS_VALUE computations require more than TIMEOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * msecs to complete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.concurrent.locks.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public final class CancelledLockLoops {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static final Random rng = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static boolean print = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static final int ITERS = 1000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static final long TIMEOUT = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        int maxThreads = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        if (args.length > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            maxThreads = Integer.parseInt(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        print = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        for (int i = 2; i <= maxThreads; i += (i+1) >>> 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            System.out.print("Threads: " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                new ReentrantLockLoop(i).test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            catch(BrokenBarrierException bb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                // OK, ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            Thread.sleep(TIMEOUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static final class ReentrantLockLoop implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        private int v = rng.nextInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        private int completed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        private volatile int result = 17;
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
        private final int nthreads;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        ReentrantLockLoop(int nthreads) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            this.nthreads = nthreads;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            barrier = new CyclicBarrier(nthreads+1, timer);
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
            Thread[] threads = new Thread[nthreads];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            for (int i = 0; i < threads.length; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                threads[i] = new Thread(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            for (int i = 0; i < threads.length; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                threads[i].start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            Thread[] cancels = (Thread[]) (threads.clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            Collections.shuffle(Arrays.asList(cancels), rng);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            Thread.sleep(TIMEOUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            for (int i = 0; i < cancels.length-2; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                cancels[i].interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                // make sure all OK even when cancellations spaced out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                if ( (i & 3) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    Thread.sleep(1 + rng.nextInt(10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (print) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                long time = timer.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                double secs = (double)(time) / 1000000000.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                System.out.println("\t " + secs + "s run time");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                c = completed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 2
diff changeset
   118
            if (c != 2)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                throw new Error("Completed != 2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            int r = result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            if (r == 0) // avoid overoptimization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                System.out.println("useless result: " + r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        public final void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                int sum = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                int x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                int n = ITERS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        v = x = LoopHelpers.compute1(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                        lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    sum += LoopHelpers.compute2(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                } while (n-- > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                if (n <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        ++completed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                result += sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
}