jdk/test/java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 7518 0282db800fe1
child 18160 da854405dc59
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7518
diff changeset
     2
 * Copyright (c) 2007, 2010, 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 6450200 6450205 6450207 6450211
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test proper handling of tasks that terminate abruptly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @run main/othervm -XX:-UseVMInterruptibleIO ThrowingTasks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.concurrent.atomic.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class ThrowingTasks {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    38
    static final Random rnd = new Random();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    @SuppressWarnings("serial")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static class UncaughtExceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        extends ConcurrentHashMap<Class<?>, Integer> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        void inc(Class<?> key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                Integer i = get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                if (i == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                    if (putIfAbsent(key, 1) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                    if (replace(key, i, i + 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    @SuppressWarnings("serial")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static class UncaughtExceptionsTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        extends Hashtable<Class<?>, Integer> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        synchronized void inc(Class<?> key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            Integer i = get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            put(key, (i == null) ? 1 : i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    68
    static final UncaughtExceptions uncaughtExceptions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        = new UncaughtExceptions();
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    70
    static final UncaughtExceptionsTable uncaughtExceptionsTable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        = new UncaughtExceptionsTable();
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    72
    static final AtomicLong totalUncaughtExceptions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        = new AtomicLong(0);
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    74
    static final CountDownLatch uncaughtExceptionsLatch
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        = new CountDownLatch(24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    77
    static final Thread.UncaughtExceptionHandler handler
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        = new Thread.UncaughtExceptionHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                public void uncaughtException(Thread t, Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    check(! Thread.currentThread().isInterrupted());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    totalUncaughtExceptions.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    uncaughtExceptions.inc(e.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    uncaughtExceptionsTable.inc(e.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    uncaughtExceptionsLatch.countDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    87
    static final ThreadGroup tg = new ThreadGroup("Flaky");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    89
    static final ThreadFactory tf = new ThreadFactory() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            public Thread newThread(Runnable r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                Thread t = new Thread(tg, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                t.setUncaughtExceptionHandler(handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    96
    static final RuntimeException rte = new RuntimeException();
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    97
    static final Error error = new Error();
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    98
    static final Throwable weird = new Throwable();
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    99
    static final Exception checkedException = new Exception();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    static class Thrower implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        Throwable t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        Thrower(Throwable t) { this.t = t; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        @SuppressWarnings("deprecation")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        public void run() { if (t != null) Thread.currentThread().stop(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   108
    static final Thrower noThrower      = new Thrower(null);
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   109
    static final Thrower rteThrower     = new Thrower(rte);
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   110
    static final Thrower errorThrower   = new Thrower(error);
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   111
    static final Thrower weirdThrower   = new Thrower(weird);
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   112
    static final Thrower checkedThrower = new Thrower(checkedException);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   114
    static final List<Thrower> throwers = Arrays.asList(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        noThrower, rteThrower, errorThrower, weirdThrower, checkedThrower);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    static class Flaky implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        final Runnable beforeExecute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        final Runnable execute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        Flaky(Runnable beforeExecute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
              Runnable execute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            this.beforeExecute = beforeExecute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            this.execute = execute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        public void run() { execute.run(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    static final List<Flaky> flakes = new ArrayList<Flaky>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        for (Thrower x : throwers)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            for (Thrower y : throwers)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                flakes.add(new Flaky(x, y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        Collections.shuffle(flakes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    static final CountDownLatch allStarted = new CountDownLatch(flakes.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    static final CountDownLatch allContinue = new CountDownLatch(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    static class PermissiveSecurityManger extends SecurityManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        public void checkPermission(Permission p) { /* bien sur, Monsieur */ }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    static void checkTerminated(ThreadPoolExecutor tpe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            check(tpe.getQueue().isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            check(tpe.isShutdown());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            check(tpe.isTerminated());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            check(! tpe.isTerminating());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            equal(tpe.getActiveCount(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            equal(tpe.getPoolSize(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            equal(tpe.getTaskCount(), tpe.getCompletedTaskCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            check(tpe.awaitTermination(0, TimeUnit.SECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    static class CheckingExecutor extends ThreadPoolExecutor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        CheckingExecutor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            super(10, 10,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                  1L, TimeUnit.HOURS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                  new LinkedBlockingQueue<Runnable>(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                  tf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        @Override protected void beforeExecute(Thread t, Runnable r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            allStarted.countDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            if (allStarted.getCount() < getCorePoolSize())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                try { allContinue.await(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                catch (InterruptedException x) { unexpected(x); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            beforeExecuteCount.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            check(! isTerminated());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            ((Flaky)r).beforeExecute.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        @Override protected void afterExecute(Runnable r, Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            //System.out.println(tg.activeCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            afterExecuteCount.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            check(((Thrower)((Flaky)r).execute).t == t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            check(! isTerminated());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        @Override protected void terminated() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                terminatedCount.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                if (rnd.nextBoolean()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    check(isShutdown());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    check(isTerminating());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    check(! isTerminated());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    check(! awaitTermination(0L, TimeUnit.MINUTES));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    static final AtomicInteger beforeExecuteCount = new AtomicInteger(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    static final AtomicInteger afterExecuteCount  = new AtomicInteger(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    static final AtomicInteger terminatedCount    = new AtomicInteger(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (rnd.nextBoolean())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            System.setSecurityManager(new PermissiveSecurityManger());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        CheckingExecutor tpe = new CheckingExecutor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        for (Runnable task : flakes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            tpe.execute(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (rnd.nextBoolean()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            allStarted.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            equal(tpe.getTaskCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                  (long) flakes.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            equal(tpe.getCompletedTaskCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                  (long) flakes.size() - tpe.getCorePoolSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        allContinue.countDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        //System.out.printf("thread count = %d%n", tg.activeCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        uncaughtExceptionsLatch.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        while (tg.activeCount() != tpe.getCorePoolSize() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
               tg.activeCount() != tpe.getCorePoolSize())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            Thread.sleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        equal(tg.activeCount(), tpe.getCorePoolSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        tpe.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        check(tpe.awaitTermination(10L, TimeUnit.MINUTES));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        checkTerminated(tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        //while (tg.activeCount() > 0) Thread.sleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        //System.out.println(uncaughtExceptions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        List<Map<Class<?>, Integer>> maps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            = new ArrayList<Map<Class<?>, Integer>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        maps.add(uncaughtExceptions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        maps.add(uncaughtExceptionsTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        for (Map<Class<?>, Integer> map : maps) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            equal(map.get(Exception.class), throwers.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            equal(map.get(weird.getClass()), throwers.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            equal(map.get(Error.class), throwers.size() + 1 + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            equal(map.get(RuntimeException.class), throwers.size() + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            equal(map.size(), 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        equal(totalUncaughtExceptions.get(), 4L*throwers.size() + 4L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        equal(beforeExecuteCount.get(), flakes.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        equal(afterExecuteCount.get(), throwers.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        equal(tpe.getCompletedTaskCount(), (long) flakes.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        equal(terminatedCount.get(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        // check for termination operation idempotence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        tpe.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        tpe.shutdownNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        check(tpe.awaitTermination(10L, TimeUnit.MINUTES));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        checkTerminated(tpe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        equal(terminatedCount.get(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
}