jdk/test/java/lang/System/finalization/FinThreads.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 5506 202f599c92aa
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
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) 1998, 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
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
   @bug 4026895
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
   @summary Ensure that System.runFinalization does not run finalizers in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
            thread that invokes it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
public class FinThreads {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    static Thread mainThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    static Object lock = new Object();    /* Protects following two fields */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    static Thread finalizerThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    static Thread finalizedBy = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static class Foo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        boolean catchFinalizer = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        /* Instances are only created in an auxiliary thread, in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
           guard against stray references from the current thread's stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
         */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 5506
diff changeset
    47
        public static void create(final boolean catchFinalizer)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            throws InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            Thread t = new Thread(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                    new Foo(catchFinalizer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            t.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        public Foo(boolean catchFinalizer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            this.catchFinalizer = catchFinalizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        public void finalize() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            if (catchFinalizer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                boolean gotFinalizer = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    if (finalizerThread == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                        finalizerThread = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                        gotFinalizer = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                if (gotFinalizer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    System.err.println("Caught finalizer thread; sleeping...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    Thread.sleep(Long.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    finalizedBy = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                System.err.println("Test object finalized by " + finalizedBy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static void alarm(final Thread sleeper, final long delay)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        throws InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        Thread t = new Thread(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    Thread.sleep(delay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    System.err.println("Waking " + sleeper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    sleeper.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                } catch (InterruptedException x) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        t.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        mainThread = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        /* Find the finalizer thread and put it to sleep */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            Foo.create(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                if (finalizerThread != null) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        /* Now create a finalizable object and run finalization */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        alarm(finalizerThread, 5000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        Foo.create(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            System.runFinalization();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                if (finalizedBy != null) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (finalizedBy == mainThread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new Exception("Finalizer run in main thread");
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
}