Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame^] | 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 | import java.util.Arrays; |
| 18 | |
| 19 | public class Main { |
| 20 | public static void main(String[] args) throws Exception { |
| 21 | System.loadLibrary(args[1]); |
| 22 | |
| 23 | doTest(); |
| 24 | } |
| 25 | |
| 26 | private static void doTest() throws Exception { |
| 27 | Thread t1 = Thread.currentThread(); |
| 28 | Thread t2 = getCurrentThread(); |
| 29 | |
| 30 | if (t1 != t2) { |
| 31 | throw new RuntimeException("Expected " + t1 + " but got " + t2); |
| 32 | } |
| 33 | System.out.println("currentThread OK"); |
| 34 | |
| 35 | printThreadInfo(t1); |
| 36 | printThreadInfo(null); |
| 37 | |
| 38 | Thread t3 = new Thread("Daemon Thread"); |
| 39 | t3.setDaemon(true); |
| 40 | // Do not start this thread, yet. |
| 41 | printThreadInfo(t3); |
| 42 | // Start, and wait for it to die. |
| 43 | t3.start(); |
| 44 | t3.join(); |
| 45 | Thread.currentThread().sleep(500); // Wait a little bit. |
| 46 | // Thread has died, check that we can still get info. |
| 47 | printThreadInfo(t3); |
| 48 | } |
| 49 | |
| 50 | private static void printThreadInfo(Thread t) { |
| 51 | Object[] threadInfo = getThreadInfo(t); |
| 52 | if (threadInfo == null || threadInfo.length != 5) { |
| 53 | System.out.println(Arrays.toString(threadInfo)); |
| 54 | throw new RuntimeException("threadInfo length wrong"); |
| 55 | } |
| 56 | |
| 57 | System.out.println(threadInfo[0]); // Name |
| 58 | System.out.println(threadInfo[1]); // Priority |
| 59 | System.out.println(threadInfo[2]); // Daemon |
| 60 | System.out.println(threadInfo[3]); // Threadgroup |
| 61 | System.out.println(threadInfo[4] == null ? "null" : threadInfo[4].getClass()); // Context CL. |
| 62 | } |
| 63 | |
| 64 | private static native Thread getCurrentThread(); |
| 65 | private static native Object[] getThreadInfo(Thread t); |
| 66 | } |