Fix the thread test to not be racy.
We shouldn't assume we know what order threads will be started in.
Running a debug build, it sometimes takes > 1ms to start the next
thread.
Change-Id: Icb7792f83809ad414cfcdf0828cdc198698deaf1
diff --git a/test/051-thread/src/Main.java b/test/051-thread/src/Main.java
index 7cc3db4..95ec1a1 100644
--- a/test/051-thread/src/Main.java
+++ b/test/051-thread/src/Main.java
@@ -1,22 +1,28 @@
// Copyright 2006 The Android Open Source Project
+import java.util.ArrayList;
+
/**
* Test some basic thread stuff.
*/
public class Main {
- public static void main(String[] args) {
+ public static void main(String[] args) throws Exception {
System.out.println("Initializing System.out...");
+ MyThread[] threads = new MyThread[512];
for (int i = 0; i < 512; i++) {
- MyThread myThread = new MyThread();
- myThread.start();
- try {
- Thread.sleep(1);
- } catch (InterruptedException ie) {
- ie.printStackTrace();
- }
+ threads[i] = new MyThread();
}
+ for (MyThread thread : threads) {
+ thread.start();
+ }
+ for (MyThread thread : threads) {
+ thread.join();
+ }
+
+ System.out.println("Thread count: " + MyThread.mCount);
+
go();
System.out.println("thread test done");
}
@@ -42,9 +48,11 @@
* Simple thread capacity test.
*/
static class MyThread extends Thread {
- private static int mCount = 0;
+ static int mCount = 0;
public void run() {
- System.out.println("running " + (mCount++));
+ synchronized (MyThread.class) {
+ ++mCount;
+ }
}
}
}