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 Test1903 { 20 public static final Object lock = new Object(); 21 22 public static volatile boolean OTHER_THREAD_CONTINUE = true; 23 public static volatile boolean OTHER_THREAD_DID_SOMETHING = true; 24 public static volatile boolean OTHER_THREAD_STARTED = false; 25 public static volatile boolean OTHER_THREAD_RESUMED = false; 26 27 public static class OtherThread implements Runnable { 28 @Override run()29 public void run() { 30 // Wake up main thread. 31 OTHER_THREAD_STARTED = true; 32 try { 33 Suspension.suspend(Thread.currentThread()); 34 OTHER_THREAD_RESUMED = true; 35 } catch (Throwable t) { 36 System.out.println("Unexpected error occurred " + t); 37 t.printStackTrace(); 38 Runtime.getRuntime().halt(2); 39 } 40 } 41 } 42 waitFor(long millis)43 public static void waitFor(long millis) { 44 try { 45 lock.wait(millis); 46 } catch (Exception e) { 47 System.out.println("Unexpected error: " + e); 48 e.printStackTrace(); 49 } 50 } 51 waitForSuspension(Thread target)52 public static void waitForSuspension(Thread target) { 53 while (!Suspension.isSuspended(target)) { 54 waitFor(100); 55 } 56 } 57 waitForStart()58 public static void waitForStart() { 59 while (!OTHER_THREAD_STARTED) { 60 waitFor(100); 61 } 62 } 63 run()64 public static void run() { 65 synchronized (lock) { 66 Thread other = new Thread(new OtherThread(), "TARGET THREAD"); 67 try { 68 other.start(); 69 70 // Wait for the other thread to actually start doing things. 71 72 waitForStart(); 73 waitForSuspension(other); 74 75 Suspension.resume(other); 76 for (int i = 0; i < 1000; i++) { 77 waitFor(100); 78 if (OTHER_THREAD_RESUMED) { 79 return; 80 } 81 } 82 System.out.println("Failed to resume thread!"); 83 Runtime.getRuntime().halt(4); 84 } catch (Throwable t) { 85 System.out.println("something was thrown. Runtime might be in unrecoverable state: " + t); 86 t.printStackTrace(); 87 Runtime.getRuntime().halt(2); 88 } 89 } 90 } 91 } 92