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 Test1904 {
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 
26   public static class OtherThread implements Runnable {
27     @Override
run()28     public void run() {
29       OTHER_THREAD_STARTED = true;
30       while (OTHER_THREAD_CONTINUE) {
31         OTHER_THREAD_DID_SOMETHING = true;
32       }
33     }
34   }
35 
waitFor(long millis)36   public static void waitFor(long millis) {
37     try {
38       lock.wait(millis);
39     } catch (Exception e) {
40       System.out.println("Unexpected error: " + e);
41       e.printStackTrace();
42     }
43   }
44 
waitForSuspension(Thread target)45   public static void waitForSuspension(Thread target) {
46     while (!Suspension.isSuspended(target)) {
47       waitFor(100);
48     }
49   }
50 
waitForStart()51   public static void waitForStart() {
52     while (!OTHER_THREAD_STARTED) {
53       waitFor(100);
54     }
55   }
56 
57 
run()58   public static void run() {
59     synchronized (lock) {
60       Thread other = new Thread(new OtherThread(), "TARGET THREAD");
61       try {
62         other.start();
63 
64         waitForStart();
65 
66         Suspension.suspend(other);
67 
68         waitForSuspension(other);
69         OTHER_THREAD_DID_SOMETHING = false;
70         // Wait a second to see if anything happens.
71         waitFor(1000);
72 
73         if (OTHER_THREAD_DID_SOMETHING) {
74           System.out.println("Looks like other thread did something while suspended!");
75         }
76 
77         try {
78           Suspension.suspend(other);
79         } catch (Exception e) {
80           System.out.println("Got exception " + e.getMessage());
81         }
82 
83         // Resume always.
84         Suspension.resume(other);
85 
86         // Wait another second.
87         waitFor(1000);
88 
89         if (!OTHER_THREAD_DID_SOMETHING) {
90           System.out.println("Doesn't look like the thread unsuspended!");
91         }
92 
93         // Stop the other thread.
94         OTHER_THREAD_CONTINUE = false;
95         // Wait for 1 second for it to die.
96         other.join(1000);
97 
98         if (other.isAlive()) {
99           System.out.println("other thread didn't terminate in a reasonable time!");
100           Runtime.getRuntime().halt(1);
101         }
102       } catch (Throwable t) {
103         System.out.println("something was thrown. Runtime might be in unrecoverable state: " + t);
104         t.printStackTrace();
105         Runtime.getRuntime().halt(2);
106       }
107     }
108   }
109 }
110