1 /* 2 * Copyright (c) 2019, 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 /* 26 * @test HandshakeSuspendExitTest 27 * @summary This test tries to stress the handshakes with new and exiting threads while suspending them. 28 * @library /testlibrary /test/lib 29 * @build HandshakeSuspendExitTest 30 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:GuaranteedSafepointInterval=1 -XX:+HandshakeALot HandshakeSuspendExitTest 31 */ 32 33 public class HandshakeSuspendExitTest implements Runnable { 34 35 static Thread[] _suspend_threads = new Thread[16]; 36 static volatile boolean _exit_now = false; 37 static java.util.concurrent.Semaphore _sem = new java.util.concurrent.Semaphore(0); 38 39 @Override 40 public void run() { 41 _sem.release(); 42 while (!_exit_now) { 43 int i = 0; 44 for (Thread thr : _suspend_threads) { 45 if (i++ > _suspend_threads.length -2) { 46 // Leave last 2 threads running. 47 break; 48 } 49 if (Thread.currentThread() != thr) { 50 thr.suspend(); 51 thr.resume(); 52 } 53 } 54 } 55 _sem.release(); 56 } 57 58 public static void main(String... args) throws Exception { 59 HandshakeSuspendExitTest test = new HandshakeSuspendExitTest(); 60 // Fire-up suspend thread. 61 for (int i = 0; i < _suspend_threads.length; i++) { 62 _suspend_threads[i] = new Thread(test); 63 } 64 for (int i = 0; i < _suspend_threads.length; i++) { 65 _suspend_threads[i].start(); 66 } 67 // Wait for all suspend thread starting to loop. 68 for (Thread thr : _suspend_threads) { 69 _sem.acquire(); 70 } 71 72 // Fire-up exiting threads. 73 Thread[] exit_threads = new Thread[128]; 74 for (int i = 0; i < exit_threads.length; i++) { 75 exit_threads[i] = new Thread(new Runnable() { public void run() {} }); 76 exit_threads[i].start(); 77 } 78 79 // Try to suspend them. 80 for (Thread thr : exit_threads) { 81 thr.suspend(); 82 } 83 for (Thread thr : exit_threads) { 84 thr.resume(); 85 } 86 87 // Start exit and join. 88 _exit_now = true; 89 int waiting = _suspend_threads.length; 90 do { 91 for (Thread thr : _suspend_threads) { 92 thr.resume(); 93 } 94 while (_sem.tryAcquire()) { 95 --waiting; 96 } 97 } while (waiting > 0); 98 for (Thread thr : _suspend_threads) { 99 thr.join(); 100 } 101 for (Thread thr : exit_threads) { 102 thr.join(); 103 } 104 } 105 }