PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/test/051-thread/src/Main.java

https://gitlab.com/androidopensourceproject/platform-art
Java | 235 lines | 183 code | 31 blank | 21 comment | 23 complexity | 7f390b3e4e808c14e6a6855c12fce218 MD5 | raw file
  1. /*
  2. * Copyright (C) 2006 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. import java.util.ArrayList;
  17. import java.util.Arrays;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. /**
  21. * Test some basic thread stuff.
  22. */
  23. public class Main {
  24. public static void main(String[] args) throws Exception {
  25. System.loadLibrary(args[0]);
  26. System.out.println("thread test starting");
  27. testThreadCapacity();
  28. testThreadDaemons();
  29. testSleepZero();
  30. testSetName();
  31. testThreadPriorities();
  32. testMainThreadGroup();
  33. testMainThreadAllStackTraces();
  34. System.out.println("thread test done");
  35. }
  36. /*
  37. * Simple thread capacity test.
  38. */
  39. private static void testThreadCapacity() throws Exception {
  40. TestCapacityThread[] threads = new TestCapacityThread[128];
  41. for (int i = 0; i < threads.length; i++) {
  42. threads[i] = new TestCapacityThread();
  43. }
  44. for (TestCapacityThread thread : threads) {
  45. thread.start();
  46. }
  47. for (TestCapacityThread thread : threads) {
  48. thread.join();
  49. }
  50. System.out.println("testThreadCapacity thread count: " + TestCapacityThread.mCount);
  51. }
  52. private static class TestCapacityThread extends Thread {
  53. static int mCount = 0;
  54. public void run() {
  55. synchronized (TestCapacityThread.class) {
  56. ++mCount;
  57. }
  58. try {
  59. sleep(1000);
  60. } catch (Exception ex) {
  61. }
  62. }
  63. }
  64. private static void testThreadDaemons() {
  65. Thread t = new Thread(null, new TestDaemonThread(), "TestDaemonThread", 7168);
  66. t.setDaemon(false);
  67. System.out.print("testThreadDaemons starting thread '" + t.getName() + "'\n");
  68. t.start();
  69. try {
  70. t.join();
  71. } catch (InterruptedException ex) {
  72. ex.printStackTrace(System.out);
  73. }
  74. System.out.print("testThreadDaemons finished\n");
  75. }
  76. private static class TestDaemonThread implements Runnable {
  77. public void run() {
  78. System.out.print("testThreadDaemons @ Thread running\n");
  79. try {
  80. Thread.currentThread().setDaemon(true);
  81. System.out.print("testThreadDaemons @ FAILED: setDaemon() succeeded\n");
  82. } catch (IllegalThreadStateException itse) {
  83. System.out.print("testThreadDaemons @ Got expected setDaemon exception\n");
  84. }
  85. try {
  86. Thread.sleep(2000);
  87. }
  88. catch (InterruptedException ie) {
  89. System.out.print("testThreadDaemons @ Interrupted!\n");
  90. }
  91. finally {
  92. System.out.print("testThreadDaemons @ Thread bailing\n");
  93. }
  94. }
  95. }
  96. private static void testSleepZero() throws Exception {
  97. Thread.currentThread().interrupt();
  98. try {
  99. Thread.sleep(0);
  100. throw new AssertionError("unreachable");
  101. } catch (InterruptedException e) {
  102. if (Thread.currentThread().isInterrupted()) {
  103. throw new AssertionError("thread is interrupted");
  104. }
  105. }
  106. System.out.print("testSleepZero finished\n");
  107. }
  108. private static void testSetName() throws Exception {
  109. System.out.print("testSetName starting\n");
  110. Thread thread = new Thread() {
  111. @Override
  112. public void run() {
  113. System.out.print("testSetName running\n");
  114. }
  115. };
  116. thread.start();
  117. thread.setName("HelloWorld"); // b/17302037 hang if setName called after start
  118. if (!thread.getName().equals("HelloWorld")) {
  119. throw new AssertionError("Unexpected thread name: " + thread.getName());
  120. }
  121. thread.join();
  122. if (!thread.getName().equals("HelloWorld")) {
  123. throw new AssertionError("Unexpected thread name after join: " + thread.getName());
  124. }
  125. System.out.print("testSetName finished\n");
  126. }
  127. private static void testThreadPriorities() throws Exception {
  128. System.out.print("testThreadPriorities starting\n");
  129. PriorityStoringThread t1 = new PriorityStoringThread(false);
  130. t1.setPriority(Thread.MAX_PRIORITY);
  131. t1.start();
  132. t1.join();
  133. if (supportsThreadPriorities() && (t1.getNativePriority() != Thread.MAX_PRIORITY)) {
  134. System.out.print("thread priority for t1 was " + t1.getNativePriority() +
  135. " [expected Thread.MAX_PRIORITY]\n");
  136. }
  137. PriorityStoringThread t2 = new PriorityStoringThread(true);
  138. t2.start();
  139. t2.join();
  140. if (supportsThreadPriorities() && (t2.getNativePriority() != Thread.MAX_PRIORITY)) {
  141. System.out.print("thread priority for t2 was " + t2.getNativePriority() +
  142. " [expected Thread.MAX_PRIORITY]\n");
  143. }
  144. System.out.print("testThreadPriorities finished\n");
  145. }
  146. private static void testMainThreadGroup() {
  147. Thread threads[] = new Thread[10];
  148. Thread current = Thread.currentThread();
  149. current.getThreadGroup().enumerate(threads);
  150. for (Thread t : threads) {
  151. if (t == current) {
  152. System.out.println("Found current Thread in ThreadGroup");
  153. return;
  154. }
  155. }
  156. throw new RuntimeException("Did not find main thread: " + Arrays.toString(threads));
  157. }
  158. private static void testMainThreadAllStackTraces() {
  159. StackTraceElement[] trace = Thread.getAllStackTraces().get(Thread.currentThread());
  160. if (trace == null) {
  161. throw new RuntimeException("Did not find main thread: " + Thread.getAllStackTraces());
  162. }
  163. List<StackTraceElement> list = Arrays.asList(trace);
  164. Iterator<StackTraceElement> it = list.iterator();
  165. while (it.hasNext()) {
  166. StackTraceElement ste = it.next();
  167. if (ste.getClassName().equals("Main")) {
  168. if (!ste.getMethodName().equals("testMainThreadAllStackTraces")) {
  169. throw new RuntimeException(list.toString());
  170. }
  171. StackTraceElement ste2 = it.next();
  172. if (!ste2.getClassName().equals("Main")) {
  173. throw new RuntimeException(list.toString());
  174. }
  175. if (!ste2.getMethodName().equals("main")) {
  176. throw new RuntimeException(list.toString());
  177. }
  178. System.out.println("Found expected stack in getAllStackTraces()");
  179. return;
  180. }
  181. }
  182. throw new RuntimeException(list.toString());
  183. }
  184. private static native int getNativePriority();
  185. private static native boolean supportsThreadPriorities();
  186. static class PriorityStoringThread extends Thread {
  187. private final boolean setPriority;
  188. private volatile int nativePriority;
  189. public PriorityStoringThread(boolean setPriority) {
  190. this.setPriority = setPriority;
  191. this.nativePriority = -1;
  192. }
  193. @Override
  194. public void run() {
  195. if (setPriority) {
  196. setPriority(Thread.MAX_PRIORITY);
  197. }
  198. nativePriority = Main.getNativePriority();
  199. }
  200. public int getNativePriority() {
  201. return nativePriority;
  202. }
  203. }
  204. }