Debian Codegraph @

test/1906-suspend-list-me-first/src/art/Test1906.java

raw
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 Test1906 {
20 public static final Object lock = new Object();
21
22 public static volatile boolean SECOND_THREAD_RUN = true;
23 public static volatile boolean SECOND_THREAD_RUNNING = false;
24
25 public static void waitFor(long millis) {
26 try {
27 lock.wait(millis);
28 } catch (Exception e) {
29 System.out.println("Unexpected error: " + e);
30 e.printStackTrace();
31 }
32 }
33
34 public static void waitForSuspension(Thread target) {
35 while (!Suspension.isSuspended(target)) {
36 waitFor(100);
37 }
38 }
39
40 public static void run() {
41 synchronized (lock) {
42 final Thread second_thread = new Thread(
43 () -> {
44 while (SECOND_THREAD_RUN) { SECOND_THREAD_RUNNING = true; }
45 },
46 "SECONDARY THREAD");
47 Thread self_thread = new Thread(
48 () -> {
49 try {
50 // Wait for second thread to start doing stuff.
51 while (!SECOND_THREAD_RUNNING) { }
52 Suspension.suspendList(Thread.currentThread(), second_thread);
53 } catch (Throwable t) {
54 System.out.println("Unexpected error occurred " + t);
55 t.printStackTrace();
56 Runtime.getRuntime().halt(2);
57 }
58 },
59 "TARGET THREAD");
60 try {
61 second_thread.start();
62 self_thread.start();
63
64 waitForSuspension(self_thread);
65
66 // Wait to see if second thread is running.
67 SECOND_THREAD_RUNNING = false;
68 waitFor(1000);
69
70 if (SECOND_THREAD_RUNNING) {
71 System.out.println("Second thread running after first thread suspended self!");
72 } else {
73 System.out.println("Second thread suspended before first thread suspended self!");
74 }
75
76 Suspension.resume(self_thread);
77 waitForSuspension(second_thread);
78 Suspension.resume(second_thread);
79 self_thread.join();
80 SECOND_THREAD_RUN = false;
81 second_thread.join();
82 } catch (Throwable t) {
83 System.out.println("something was thrown. Runtime might be in unrecoverable state: " + t);
84 t.printStackTrace();
85 Runtime.getRuntime().halt(2);
86 }
87 }
88 }
89 }