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