Path: blob/master/test/hotspot/jtreg/compiler/c2/InterruptedTest.java
41149 views
/*1* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 677268326* @summary Thread.isInterrupted() fails to return true on multiprocessor PC27*28* @run main/othervm compiler.c2.InterruptedTest 10029*/3031package compiler.c2;3233public class InterruptedTest {3435public static void main(String[] args) throws Exception {36/* The value of the threshold determines for how many seconds37* the main thread must wait for the worker thread. On highly38* loaded systems it can take a while until the worker thread39* obtains CPU time and is able to check if it was interrupted40* by the main thread. The higher the threshold the likelier41* the worker thread can check if it was interrupted (that is42* required for successul test execution).43*/44int threshold = 100;4546if (args.length != 1) {47System.out.println("Incorrect number of arguments");48System.exit(1);49}5051try {52threshold = Integer.parseInt(args[0]);53} catch (NumberFormatException e) {54System.out.println("Invalid argument format");55System.exit(1);56}5758if (threshold < 1) {59System.out.println("Threshold must be at least 1");60System.exit(1);61}6263Thread workerThread = new Thread("worker") {64public void run() {65System.out.println("Worker thread: running...");66while (!Thread.currentThread().isInterrupted()) {67}68System.out.println("Worker thread: bye");69}70};71System.out.println("Main thread: starts a worker thread...");72workerThread.start();73System.out.println("Main thread: waits 5 seconds after starting the worker thread");74workerThread.join(5000); // Wait 5 sec to let run() method to be compiled7576int ntries = 0;77while (workerThread.isAlive() && ntries < threshold) {78System.out.println("Main thread: interrupts the worker thread...");79workerThread.interrupt();80if (workerThread.isInterrupted()) {81System.out.println("Main thread: worker thread is interrupted");82}83ntries++;84System.out.println("Main thread: waits 1 second for the worker thread to die...");85workerThread.join(1000); // Wait 1 sec and try again86}8788if (ntries == threshold) {89System.out.println("Main thread: the worker thread did not die after " +90ntries + " seconds have elapsed");91System.exit(97);92}9394System.out.println("Main thread: bye");95}96}979899