1 /*
2  * Copyright (C) 2019 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 import java.lang.reflect.Method;
20 import java.lang.reflect.Proxy;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24 import java.util.concurrent.CountDownLatch;
25 
26 public class Test1962 {
doNothing()27   public static void doNothing() {
28     // We set a breakpoint here!
29   }
run()30   public static void run() throws Exception {
31     doTest();
32   }
33 
HandleEvent(Thread t, List<String> events)34   public static void HandleEvent(Thread t, List<String> events) {
35     events.add("Hit event on " + t.getName());
36   }
37 
doTest()38   public static void doTest() throws Exception {
39     setupTest();
40 
41     final int NUM_THREADS = 2;
42     List<String> t1Events = new ArrayList<>();
43     List<String> t2Events = new ArrayList<>();
44     final CountDownLatch threadResumeLatch = new CountDownLatch(1);
45     final CountDownLatch threadStartLatch = new CountDownLatch(NUM_THREADS);
46     Runnable threadRun = () -> {
47       try {
48         threadStartLatch.countDown();
49         threadResumeLatch.await();
50         doNothing();
51       } catch (Exception e) {
52         throw new Error("Failed at something", e);
53       }
54     };
55     Thread T1 = new Thread(threadRun, "T1 Thread");
56     Thread T2 = new Thread(threadRun, "T2 Thread");
57     T1.start();
58     T2.start();
59     // Wait for both threads to have started.
60     threadStartLatch.await();
61     // Tell the threads to notify us when the doNothing method exits
62     Method target = Test1962.class.getDeclaredMethod("doNothing");
63     setupThread(T1, t1Events, target);
64     setupThread(T2, t2Events, target);
65     // Let the threads go.
66     threadResumeLatch.countDown();
67     // Wait for the threads to finish.
68     T1.join();
69     T2.join();
70     // Print out the events each of the threads performed.
71     System.out.println("Events on thread 1:");
72     for (String s : t1Events) {
73       System.out.println("\t" + s);
74     }
75     System.out.println("Events on thread 2:");
76     for (String s : t2Events) {
77       System.out.println("\t" + s);
78     }
79   }
80 
setupTest()81   public static native void setupTest();
setupThread(Thread t, List<String> events, Method target)82   public static native void setupThread(Thread t, List<String> events, Method target);
83 }
84