Herong's Tutorial Notes on JVM
Dr. Herong Yang, Version 3.00, 2007

Maximum Number of Threads

Thread Testing Programs

To find out the maximum number of threads we can run, I wrote the following program:

/**
 * CrashThread.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.util.*;
import java.text.*;
class CrashThread extends Thread {
   public static void main(String[] a) {
      Thread t;
      int m = 16;
      Date now;
      DateFormat df = DateFormat.getTimeInstance();
      for (int n=1; n<=m; n++) {
      	 now = new Date();
         t = new CrashThread();
         t.start();
         System.out.println(df.format(now) + " Launched threed "+n);
      }
   }
   public void run() {
      while (true);
   }
}

The program is a simple thread with infinite while loop in the run() method. The main() method is included in the thread to avoid writing another class to launch the thread repeatedly. The program was compiled with J2SDK 1.4.0_02, and executed with the same memory options: -Xms2m -Xmx64m.

Output on JRockit JVM:

10:28:21 AM Launched threed 1
......
10:28:22 AM Launched threed 16

Output on HotSpot Client:

10:30:06 AM Launched threed 1
......
10:30:09 AM Launched threed 16

Output on HotSpot Server:

10:31:56 AM Launched threed 1
......
10:31:58 AM Launched threed 16

As you can see, all 3 JVMs had no problems launching 16 threads, and kept them running. Note that all threads are user threads by default. They continue to run even after the calling threads has been terminated.

When I increase the number of threads to 32, the executions were getting interresting:

  • JRockit JVM: After launched 32 threads, the system was getting very sluggish. The mouse pointer were very much alive, but any mouse click would take about 30 seconds for the system to respond. I was able to end the JVM process by the system task manager.
  • HotSpot Client and Server: After launched 32 threads, the system basically got jammed. Only the mouse pointer can be moved around. No response to any mouse clicks. I had to turn off the power to restart the system.

I guess the problem was in the operating system (Windows 2000). It failed to treat the running JVM process as one single process event it had 32 threads running inside. The operating system should be able to put the JVM process in wait mode when its CPU time share is used up, and process my mouse clicks before returning back to the JVM process. Could some one verify this on a UNIX system?

Thread Testing with JDK 1.6.0

After installing JDK 6u2, I tested the CrashThread program again on a Windows XP system. Here are the results.

Both HotSpot Client and HotSpot Server 1.6.0_02 have no problem with 16 threads.

With 32 threads running, HotSpot Client 1.6.0_02 took 99% of the CPU time according to the report from Windows Task Manager. The system was not jammed. But it became sluggish.

With 32 threads running, HotSpot Server 1.6.0_02 also took 99% of the CPU time according to the report from Windows Task Manager. But the system response time was much better that HotSpot client test.

With 128 threads running under HotSpot Client 1.6.0_02. The system became very sluggish. But I was able to launch other applications.

With 128 threads running under HotSpot Server 1.6.0_02. The system became sluggish. But everything is working fine.

With 512 threads running under HotSpot Server 1.6.0_02. The system became very sluggish. But the system was not totally frozen. I was still able to use a text editor to types these notes.

Obviously, there has been a major thread management improvement of JDK 1.6.0 on a Windows XP system over JDK 1.4.0 on a Windows 2000 system.

Another interesting observation was that JDK 1.6.0 took some time (about 5 minutes) to launch all 512 threads, see the timestamps recorded below. May be extra time was spend on requesting more memory from the Windows system.

10:24:08 PM Launched threed 1
10:24:08 PM Launched threed 2
10:24:08 PM Launched threed 3
10:24:08 PM Launched threed 4
...
10:28:32 PM Launched threed 489
10:28:32 PM Launched threed 490
10:28:32 PM Launched threed 491
10:28:32 PM Launched threed 492
10:28:32 PM Launched threed 493
10:28:41 PM Launched threed 494
10:28:41 PM Launched threed 495
10:28:41 PM Launched threed 496
10:28:50 PM Launched threed 497
10:28:50 PM Launched threed 498
10:28:50 PM Launched threed 499
10:28:50 PM Launched threed 500
10:28:50 PM Launched threed 501
10:28:59 PM Launched threed 502
10:28:59 PM Launched threed 503
10:28:59 PM Launched threed 504
10:28:59 PM Launched threed 505
10:28:59 PM Launched threed 506
10:28:59 PM Launched threed 507
10:28:59 PM Launched threed 508
10:28:59 PM Launched threed 509
10:28:59 PM Launched threed 510
10:28:59 PM Launched threed 511
10:28:59 PM Launched threed 512
Dr. Herong Yang, updated in 2007
Herong's Tutorial Notes on JVM - Maximum Number of Threads