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