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.setName() at thread exit. 28 * @run main/othervm -Xlog:thread+smr=debug SetNameAtExit 29 */ 30 31 import java.util.concurrent.CountDownLatch; 32 33 public class SetNameAtExit 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 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 53 public static void main(String[] args) { 54 SetNameAtExit threads[] = new SetNameAtExit[N_THREADS]; 55 56 for (int i = 0; i < N_THREADS; i++ ) { 57 threads[i] = new SetNameAtExit(); 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 // This interrupt() call will break the worker out 65 // of the exitSyncObj.await() call and the setName() 66 // calls will come in during thread exit. 67 threads[i].interrupt(); 68 for (; late_count <= N_LATE_CALLS; late_count++) { 69 threads[i].setName("T" + i + "-" + late_count); 70 71 if (!threads[i].isAlive()) { 72 // Done with Thread.setName() calls since 73 // thread is not alive. 74 break; 75 } 76 } 77 } catch (InterruptedException e) { 78 throw new Error("Unexpected: " + e); 79 } 80 81 System.out.println("INFO: thread #" + i + ": made " + late_count + 82 " late calls to java.lang.Thread.setName()"); 83 System.out.println("INFO: thread #" + i + ": N_LATE_CALLS==" + 84 N_LATE_CALLS + " value is " + 85 ((late_count >= N_LATE_CALLS) ? "NOT " : "") + 86 "large enough to cause a Thread.setName() " + 87 "call after thread exit."); 88 89 try { 90 threads[i].join(); 91 } catch (InterruptedException e) { 92 throw new Error("Unexpected: " + e); 93 } 94 threads[i].setName("T" + i + "-done"); 95 if (threads[i].isAlive()) { 96 throw new Error("Expected !Thread.isAlive() after thread #" + 97 i + " has been join()'ed"); 98 } 99 } 100 101 String cmd = System.getProperty("sun.java.command"); 102 if (cmd != null && !cmd.startsWith("com.sun.javatest.regtest.agent.MainWrapper")) { 103 // Exit with success in a non-JavaTest environment: 104 System.exit(0); 105 } 106 } 107 }