1 /* 2 * Copyright (c) 2017, Oracle and/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 8167108 27 * @summary Stress test java.lang.Thread.interrupt() at thread exit. 28 * @run main/othervm -Xlog:thread+smr=debug InterruptAtExit 29 */ 30 31 import java.util.concurrent.CountDownLatch; 32 33 public class InterruptAtExit extends Thread { 34 final static int N_THREADS = 32; 35 final static int N_LATE_CALLS = 1000; 36 37 public CountDownLatch exitSyncObj = new CountDownLatch(1); 38 public CountDownLatch startSyncObj = new CountDownLatch(1); 39 40 @Override run()41 public void run() { 42 // Tell main thread we have started. 43 startSyncObj.countDown(); 44 try { 45 // Wait for main thread to interrupt us so we 46 // can race to exit. 47 exitSyncObj.await(); 48 } catch (InterruptedException e) { 49 // ignore because we expect one 50 } 51 } 52 main(String[] args)53 public static void main(String[] args) { 54 InterruptAtExit threads[] = new InterruptAtExit[N_THREADS]; 55 56 for (int i = 0; i < N_THREADS; i++ ) { 57 threads[i] = new InterruptAtExit(); 58 int late_count = 1; 59 threads[i].start(); 60 try { 61 // Wait for the worker thread to get going. 62 threads[i].startSyncObj.await(); 63 64 // The first interrupt() call will break the 65 // worker out of the exitSyncObj.await() call 66 // and the rest will come in during thread exit. 67 for (; late_count <= N_LATE_CALLS; late_count++) { 68 threads[i].interrupt(); 69 70 if (!threads[i].isAlive()) { 71 // Done with Thread.interrupt() calls since 72 // thread is not alive. 73 break; 74 } 75 } 76 } catch (InterruptedException e) { 77 throw new Error("Unexpected: " + e); 78 } 79 80 System.out.println("INFO: thread #" + i + ": made " + late_count + 81 " late calls to java.lang.Thread.interrupt()"); 82 System.out.println("INFO: thread #" + i + ": N_LATE_CALLS==" + 83 N_LATE_CALLS + " value is " + 84 ((late_count >= N_LATE_CALLS) ? "NOT " : "") + 85 "large enough to cause a Thread.interrupt() " + 86 "call after thread exit."); 87 88 try { 89 threads[i].join(); 90 } catch (InterruptedException e) { 91 throw new Error("Unexpected: " + e); 92 } 93 threads[i].interrupt(); 94 if (threads[i].isAlive()) { 95 throw new Error("Expected !Thread.isAlive() after thread #" + 96 i + " has been join()'ed"); 97 } 98 } 99 100 String cmd = System.getProperty("sun.java.command"); 101 if (cmd != null && !cmd.startsWith("com.sun.javatest.regtest.agent.MainWrapper")) { 102 // Exit with success in a non-JavaTest environment: 103 System.exit(0); 104 } 105 } 106 } 107