|
| 1 | +/* |
| 2 | + * Copyright 2023, Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @bug 8305425 |
| 27 | + * @summary Check Thread.isAlive |
| 28 | + * @run main/othervm/timeout=10 IsAlive |
| 29 | + */ |
| 30 | + |
| 31 | +public class IsAlive { |
| 32 | + |
| 33 | + static boolean spinnerDone; |
| 34 | + |
| 35 | + private static void spin() { |
| 36 | + try { |
| 37 | + while (!Thread.currentThread().isInterrupted()) { |
| 38 | + Thread.sleep(100); |
| 39 | + } |
| 40 | + } catch (InterruptedException ie) { |
| 41 | + // Do nothing, just exit |
| 42 | + } |
| 43 | + spinnerDone = true; |
| 44 | + } |
| 45 | + |
| 46 | + static volatile boolean checkerReady; |
| 47 | + |
| 48 | + private static void check(Thread t) { |
| 49 | + while (!t.isAlive()) { |
| 50 | + // Burn hard, without any sleeps. |
| 51 | + // Check that we discover the thread is alive eventually. |
| 52 | + } |
| 53 | + |
| 54 | + checkerReady = true; |
| 55 | + |
| 56 | + while (t.isAlive()) { |
| 57 | + // Burn hard, without any sleeps. |
| 58 | + // Check that we discover the thread is not alive eventually. |
| 59 | + } |
| 60 | + |
| 61 | + if (!spinnerDone) { |
| 62 | + throw new RuntimeException("Last write of terminated thread was not seen!"); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private static void assertAlive(Thread t) { |
| 67 | + if (!t.isAlive()) { |
| 68 | + throw new IllegalStateException("Thread " + t + " is not alive, but it should be"); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private static void assertNotAlive(Thread t) { |
| 73 | + if (t.isAlive()) { |
| 74 | + throw new IllegalStateException("Thread " + t + " is alive, but it should not be"); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public static void main(String args[]) throws Exception { |
| 79 | + Thread spinner = new Thread(IsAlive::spin); |
| 80 | + spinner.setName("Spinner"); |
| 81 | + spinner.setDaemon(true); |
| 82 | + |
| 83 | + Thread checker = new Thread(() -> check(spinner)); |
| 84 | + checker.setName("Checker"); |
| 85 | + checker.setDaemon(true); |
| 86 | + |
| 87 | + assertNotAlive(spinner); |
| 88 | + assertNotAlive(checker); |
| 89 | + |
| 90 | + System.out.println("Starting spinner"); |
| 91 | + spinner.start(); |
| 92 | + assertAlive(spinner); |
| 93 | + |
| 94 | + System.out.println("Starting checker"); |
| 95 | + checker.start(); |
| 96 | + assertAlive(checker); |
| 97 | + |
| 98 | + System.out.println("Waiting for checker to catch up"); |
| 99 | + while (!checkerReady) { |
| 100 | + Thread.sleep(100); |
| 101 | + } |
| 102 | + |
| 103 | + System.out.println("Interrupting and joining spinner"); |
| 104 | + spinner.interrupt(); |
| 105 | + spinner.join(); |
| 106 | + assertNotAlive(spinner); |
| 107 | + |
| 108 | + System.out.println("Joining checker"); |
| 109 | + checker.join(); |
| 110 | + assertNotAlive(checker); |
| 111 | + |
| 112 | + System.out.println("Complete"); |
| 113 | + } |
| 114 | +} |
0 commit comments