jdk/test/java/util/concurrent/ThreadPoolExecutor/ScheduledTickleService.java
author dl
Wed, 01 Dec 2010 21:46:52 +0000
changeset 7518 0282db800fe1
parent 5506 202f599c92aa
child 7668 d4a77089c587
permissions -rw-r--r--
7003745: Code style cleanups (sync from Dougs CVS) Reviewed-by: chegar, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6362121
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test one ScheduledThreadPoolExecutor extension scenario
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
// based on a test kindly provided by Holger Hoffstaette <holger@wizards.de>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import static java.util.concurrent.TimeUnit.MILLISECONDS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class ScheduledTickleService {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    // We get intermittent ClassCastException if greater than 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    // because of calls to compareTo
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    40
    private static final int concurrency = 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    // Record when tasks are done
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    43
    public static final CountDownLatch done = new CountDownLatch(concurrency);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public static void realMain(String... args) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        // our tickle service
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        ScheduledExecutorService tickleService =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            new ScheduledThreadPoolExecutor(concurrency) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                // We override decorateTask() to return a custom
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                // RunnableScheduledFuture which explicitly removes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                // itself from the queue after cancellation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                protected <V> RunnableScheduledFuture<V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                    decorateTask(Runnable runnable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                                 RunnableScheduledFuture<V> task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                    final ScheduledThreadPoolExecutor exec = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                    return new CustomRunnableScheduledFuture<V>(task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                        // delegate to wrapped task, except for:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                        public boolean cancel(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                            // cancel wrapped task & remove myself from the queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                            return (task().cancel(b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                                    && exec.remove(this));}};}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        for (int i = 0; i < concurrency; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            new ScheduledTickle(i, tickleService)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                .setUpdateInterval(25, MILLISECONDS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        done.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        tickleService.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // our Runnable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    static class ScheduledTickle implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        public volatile int failures = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // my tickle service
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        private final ScheduledExecutorService service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // remember my own scheduled ticket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        private ScheduledFuture ticket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // remember the number of times I've been tickled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        private int numTickled = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        // my private name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        private final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        public ScheduledTickle(int i, ScheduledExecutorService service) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            this.name = "Tickler-"+i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            this.service = service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // set my tickle interval; 0 to disable further tickling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        public synchronized void setUpdateInterval(long interval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                                   TimeUnit unit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            // cancel & remove previously created ticket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if (ticket != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                ticket.cancel(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                ticket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if (interval > 0 && ! service.isShutdown()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                // requeue with new interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                ticket = service.scheduleAtFixedRate(this, interval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                                                     interval, unit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        public synchronized void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                check(numTickled < 6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                numTickled++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                System.out.println(name + ": Run " + numTickled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                // tickle 3 times and then slow down
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                if (numTickled == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    System.out.println(name + ": slower please!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    this.setUpdateInterval(100, MILLISECONDS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                // ..but only 5 times max.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                else if (numTickled == 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    System.out.println(name + ": OK that's enough.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    this.setUpdateInterval(0, MILLISECONDS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    ScheduledTickleService.done.countDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            } catch (Throwable t) { unexpected(t); }
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
    // This is just a generic wrapper to make up for the private ScheduledFutureTask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    static class CustomRunnableScheduledFuture<V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        implements RunnableScheduledFuture<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        // the wrapped future
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        private RunnableScheduledFuture<V> task;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        public CustomRunnableScheduledFuture(RunnableScheduledFuture<V> task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            this.task = task;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        public RunnableScheduledFuture<V> task() { return task; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // Forwarding methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        public boolean isPeriodic()         { return task.isPeriodic(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        public boolean isCancelled()        { return task.isCancelled(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        public boolean isDone()             { return task.isDone(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        public boolean cancel(boolean b)    { return task.cancel(b); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        public long getDelay(TimeUnit unit) { return task.getDelay(unit); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        public void run()                   {        task.run(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        public V get()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            throws InterruptedException, ExecutionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return task.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        public V get(long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            throws InterruptedException, ExecutionException, TimeoutException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return task.get(timeout, unit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        public int compareTo(Delayed other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if (this == other)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            else if (other instanceof CustomRunnableScheduledFuture)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                return task.compareTo(((CustomRunnableScheduledFuture)other).task());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                return task.compareTo(other);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
}