1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package art; 18 19 public class Test1942 { run()20 public static void run() throws Exception { 21 final Thread target_thread = new Thread(() -> { 22 nativeRun(); 23 }, "target_thread"); 24 25 target_thread.start(); 26 27 // wait for the other thread to spin holding lock. 28 waitForPause(); 29 30 System.out.println("Initial state."); 31 System.out.println("isLocked() = " + isLocked()); 32 System.out.println("isSuspended(target_thread) = " + Suspension.isSuspended(target_thread)); 33 34 // Suspend it from java. 35 System.out.println("Suspend and sleep."); 36 Suspension.suspend(target_thread); 37 // Wait for the other thread to do something. 38 try { Thread.sleep(1000); } catch (Exception e) {} 39 40 System.out.println("isLocked() = " + isLocked()); 41 System.out.println("isSuspended(target_thread) = " + Suspension.isSuspended(target_thread)); 42 43 // Let it try to unlock the monitor. 44 System.out.println("Let other thread release the raw monitor."); 45 // Let the thread try to lock the monitor. 46 resume(); 47 48 // Wait for the other thread to do something. It should exit by the time this is done if it 49 // has not hit a suspend point. 50 while (isLocked()) { 51 try { Thread.sleep(1000); } catch (Exception e) {} 52 } 53 54 System.out.println("isLocked() = " + isLocked()); 55 System.out.println("isSuspended(target_thread) = " + Suspension.isSuspended(target_thread)); 56 57 // Make sure the monitor is gone. 58 grabRawMonitor(); 59 System.out.println("other thread doesn't hold lock!"); 60 61 // Resume it from java 62 System.out.println("resumed test thread"); 63 Suspension.resume(target_thread); 64 target_thread.join(); 65 } 66 nativeRun()67 public static native void nativeRun(); waitForPause()68 public static native void waitForPause(); resume()69 public static native void resume(); isLocked()70 public static native boolean isLocked(); 71 // Gets then releases raw monitor. grabRawMonitor()72 public static native void grabRawMonitor(); 73 } 74