jdk/test/java/util/concurrent/DelayQueue/Stress.java
author dl
Thu, 03 Mar 2016 10:43:07 -0800
changeset 36233 f85ed703cf7e
parent 34347 4a17f9e90a0f
child 40817 4f5fb115676d
permissions -rw-r--r--
8150523: improve jtreg test timeout handling, especially -timeout: Reviewed-by: martin, psandoz, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
60
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 60
diff changeset
     2
 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
60
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
     4
 *
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
     7
 * published by the Free Software Foundation.
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
     8
 *
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    13
 * accompanied this code).
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    14
 *
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 60
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 60
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 60
diff changeset
    21
 * questions.
60
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    22
 */
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    23
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    24
import static java.util.concurrent.TimeUnit.NANOSECONDS;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    25
import static java.util.concurrent.TimeUnit.SECONDS;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    26
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    27
import java.util.concurrent.DelayQueue;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    28
import java.util.concurrent.Delayed;
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    29
import java.util.concurrent.TimeUnit;
60
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    30
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    31
/**
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    32
 * This is not a regression test, but a stress benchmark test for
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    33
 * 6609775: Reduce context switches in DelayQueue due to signalAll
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    34
 *
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    35
 * This runs in the same wall clock time, but much reduced cpu time,
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    36
 * with the changes for 6609775.
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    37
 */
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    38
public class Stress {
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    39
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    40
    public static void main(String[] args) throws Throwable {
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    41
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    42
        final DelayQueue<Delayed> q = new DelayQueue<Delayed>();
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    43
        final long t0 = System.nanoTime();
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    44
        for (long i = 0; i < 1000; i++) {
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    45
            final long expiry = t0 + i*10L*1000L*1000L;
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    46
            q.add(new Delayed() {
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    47
                    public long getDelay(TimeUnit unit) {
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    48
                        return unit.convert(expiry - System.nanoTime(),
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    49
                                            NANOSECONDS);
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    50
                    }
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    51
                    public int compareTo(Delayed x) {
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    52
                        long d = getDelay(NANOSECONDS)
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    53
                            - x.getDelay(NANOSECONDS);
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    54
                        return d < 0 ? -1 : d > 0 ? 1 : 0; }});
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    55
        }
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    56
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    57
        for (int i = 0; i < 300; i++)
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    58
            new Thread() { public void run() {
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    59
                try {
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    60
                    while (!q.isEmpty())
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    61
                        q.poll(10L, TimeUnit.SECONDS);
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    62
                } catch (Throwable t) { t.printStackTrace(); }
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    63
            }}.start();
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    64
    }
429af02854d2 6609775: Reduce context switches in DelayQueue due to signalAll
martin
parents:
diff changeset
    65
}