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