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 Test1905 {
run()20   public static void run() throws Exception {
21     final Thread spinner = new Thread(() -> {
22       nativeSpin();
23     }, "Spinner");
24 
25     final Thread resumer = new Thread(() -> {
26       String me = Thread.currentThread().getName();
27 
28       // wait for the other thread to start spinning.
29       while (!isNativeThreadSpinning()) { }
30 
31       System.out.println(me + ": isNativeThreadSpinning() = " + isNativeThreadSpinning());
32       System.out.println(me + ": isSuspended(spinner) = " + Suspension.isSuspended(spinner));
33 
34       // Suspend it from java.
35       Suspension.suspend(spinner);
36 
37       System.out.println(me + ": Suspended spinner while native spinning");
38       System.out.println(me + ": isNativeThreadSpinning() = " + isNativeThreadSpinning());
39       System.out.println(me + ": isSuspended(spinner) = " + Suspension.isSuspended(spinner));
40 
41       // Resume it from java. It is still native spinning.
42       Suspension.resume(spinner);
43 
44       System.out.println(me + ": resumed spinner while native spinning");
45       System.out.println(me + ": isNativeThreadSpinning() = " + isNativeThreadSpinning());
46       System.out.println(me + ": isSuspended(spinner) = " + Suspension.isSuspended(spinner));
47       nativeResume();
48     }, "Resumer");
49 
50     spinner.start();
51     resumer.start();
52 
53     spinner.join();
54     resumer.join();
55   }
56 
nativeSpin()57   public static native void nativeSpin();
nativeResume()58   public static native void nativeResume();
isNativeThreadSpinning()59   public static native boolean isNativeThreadSpinning();
60 }
61