1 /*
   2  * Copyright (c) 2007, 2012, 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 
  27 package org.graalvm.compiler.jtt.threads;
  28 
  29 import org.graalvm.compiler.jtt.JTTTest;
  30 import org.junit.Assert;
  31 import org.junit.Test;
  32 
  33 //Test all, mainly monitors
  34 public class Thread_isInterrupted02 extends JTTTest {
  35 
  36     private static final Object start = new Object();
  37     private static final Object end = new Object();
  38     private static int waitTime;
  39 
  40     @SuppressWarnings("unused")
  41     public static boolean test(int i, int time) throws InterruptedException {
  42         waitTime = time;
  43         final Thread thread = new Thread();
  44         synchronized (thread) {
  45             // start the thread and wait for it
  46             thread.setDaemon(true); // in case the thread gets stuck
  47             thread.start();
  48             while (!thread.wait1Condition) {
  49                 thread.wait(10000);
  50             }
  51         }
  52         synchronized (start) {
  53             thread.interrupt();
  54             thread.sentInterrupt = true;
  55         }
  56         synchronized (end) {
  57             while (!thread.wait2Condition) {
  58                 end.wait(10000);
  59             }
  60         }
  61         return thread.interrupted;
  62     }
  63 
  64     private static class Thread extends java.lang.Thread {
  65 
  66         private boolean interrupted;
  67         private boolean sentInterrupt;
  68         private boolean wait1Condition;
  69         private boolean wait2Condition;
  70 
  71         @Override
  72         public void run() {
  73             try {
  74                 synchronized (start) {
  75                     synchronized (this) {
  76                         // signal test thread that we are running
  77                         wait1Condition = true;
  78                         notify();
  79                     }
  80                     // wait for the condition, which should be interrupted
  81                     while (!sentInterrupt) {
  82                         if (waitTime == 0) {
  83                             start.wait();
  84                         } else {
  85                             start.wait(waitTime);
  86                         }
  87                         if (Thread.interrupted()) {
  88                             throw new InterruptedException();
  89                         }
  90                     }
  91                     Assert.fail("should not reach here - was not interrupted");
  92                 }
  93             } catch (InterruptedException e) {
  94                 // interrupted successfully.
  95                 interrupted = true;
  96                 synchronized (end) {
  97                     // notify the other thread we are done
  98                     wait2Condition = true;
  99                     end.notify();
 100                 }
 101             }
 102         }
 103     }
 104 
 105     @Test(timeout = 20000)
 106     public void run0() throws Throwable {
 107         initializeForTimeout();
 108         runTest("test", 0, 0);
 109     }
 110 
 111     @Test(timeout = 20000)
 112     public void run1() throws Throwable {
 113         initializeForTimeout();
 114         runTest("test", 1, 500);
 115     }
 116 
 117 }