jdk/test/java/util/concurrent/ConcurrentLinkedQueue/LoopHelpers.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
permissions -rw-r--r--
Initial load
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * have any questions.
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
 * Misc utilities in JSR166 performance tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.concurrent.atomic.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
class LoopHelpers {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    // Some mindless computation to do between synchronizations...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * generates 32 bit pseudo-random numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * Adapted from http://www.snippets.org
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static int compute1(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        int lo = 16807 * (x & 0xFFFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        int hi = 16807 * (x >>> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        lo += (hi & 0x7FFF) << 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        if ((lo & 0x80000000) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            lo &= 0x7fffffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            ++lo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        lo += hi >>> 15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (lo == 0 || (lo & 0x80000000) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            lo &= 0x7fffffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            ++lo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        return lo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *  Computes a linear congruential random number a random number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *  of times.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public static int compute2(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        int loops = (x >>> 4) & 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        while (loops-- > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            x = (x * 2147483647) % 16807;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * An actually useful random number generator, but unsynchronized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Basically same as java.util.Random.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public static class SimpleRandom {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        private final static long multiplier = 0x5DEECE66DL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        private final static long addend = 0xBL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        private final static long mask = (1L << 48) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        static final AtomicLong seq = new AtomicLong(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        private long seed = System.nanoTime() + seq.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        public void setSeed(long s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            seed = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        public int next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            long nextseed = (seed * multiplier + addend) & mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            seed = nextseed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return ((int)(nextseed >>> 17)) & 0x7FFFFFFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public static class BarrierTimer implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        public volatile long startTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        public volatile long endTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            long t = System.nanoTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (startTime == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                startTime = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                endTime = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            startTime = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            endTime = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        public long getTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            return endTime - startTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public static String rightJustify(long n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        // There's probably a better way to do this...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        String field = "         ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        String num = Long.toString(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (num.length() >= field.length())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            return num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        StringBuffer b = new StringBuffer(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        b.replace(b.length()-num.length(), b.length(), num);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return b.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
}