1/* 2 * Copyright (c) 2008, 2016, 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 6772683 27 * @summary Thread.isInterrupted() fails to return true on multiprocessor PC 28 * 29 * @run main/othervm compiler.c2.InterruptedTest 100 30 */ 31 32package compiler.c2; 33 34public class InterruptedTest { 35 36 public static void main(String[] args) throws Exception { 37 /* The value of the threshold determines for how many seconds 38 * the main thread must wait for the worker thread. On highly 39 * loaded systems it can take a while until the worker thread 40 * obtains CPU time and is able to check if it was interrupted 41 * by the main thread. The higher the threshold the likelier 42 * the worker thread can check if it was interrupted (that is 43 * required for successul test execution). 44 */ 45 int threshold = 100; 46 47 if (args.length != 1) { 48 System.out.println("Incorrect number of arguments"); 49 System.exit(1); 50 } 51 52 try { 53 threshold = Integer.parseInt(args[0]); 54 } catch (NumberFormatException e) { 55 System.out.println("Invalid argument format"); 56 System.exit(1); 57 } 58 59 if (threshold < 1) { 60 System.out.println("Threshold must be at least 1"); 61 System.exit(1); 62 } 63 64 Thread workerThread = new Thread("worker") { 65 public void run() { 66 System.out.println("Worker thread: running..."); 67 while (!Thread.currentThread().isInterrupted()) { 68 } 69 System.out.println("Worker thread: bye"); 70 } 71 }; 72 System.out.println("Main thread: starts a worker thread..."); 73 workerThread.start(); 74 System.out.println("Main thread: waits 5 seconds after starting the worker thread"); 75 workerThread.join(5000); // Wait 5 sec to let run() method to be compiled 76 77 int ntries = 0; 78 while (workerThread.isAlive() && ntries < threshold) { 79 System.out.println("Main thread: interrupts the worker thread..."); 80 workerThread.interrupt(); 81 if (workerThread.isInterrupted()) { 82 System.out.println("Main thread: worker thread is interrupted"); 83 } 84 ntries++; 85 System.out.println("Main thread: waits 1 second for the worker thread to die..."); 86 workerThread.join(1000); // Wait 1 sec and try again 87 } 88 89 if (ntries == threshold) { 90 System.out.println("Main thread: the worker thread did not die after " + 91 ntries + " seconds have elapsed"); 92 System.exit(97); 93 } 94 95 System.out.println("Main thread: bye"); 96 } 97} 98