JVM Tutorials - Herong's Tutorial Examples - Version 4.23, by Dr. Herong Yang
Two-Thread Process on 2-CPU-4-Thread Machine
This section provides a tutorial example on how a two-threaded process behaves on a 2-CPU-4-Thread architecture. Windows will only allocate 2 primary CPU-Threads to the process.
Now, let's continue with a 2-thread process.
1. Close all applications on the system.
2. Launch MultithreadingCpuRunner.java with 2 threads. The performances is almost doubled comparing to the 1 thread execution in the previous tutorial.
C:\>"\Program Files\Java\jdk1.7.0_45\bin\java" MultithreadingCpuRunner 1000 2 Thread parameter: 1000 Number of threads: 2 Seconds | Productivity per thread | Total | Average ... 146 | 833 833 | 1666 | 1665 147 | 832 833 | 1665 | 1665 148 | 839 838 | 1677 | 1665 149 | 830 832 | 1662 | 1665 150 | 839 838 | 1677 | 1665 ...
3. View individual CPU-Thread usage on the Performance tab of Task Manager. You will see that the running process is being distributed between 2 CPU-Threads with 2 other CPU-Threads doing nothing:
CPU-Thread 1 - 100% used (approximately) CPU-Thread 2 - 0% used CPU-Thread 3 - 100% used (approximately) CPU-Thread 4 - 0% used
4. View total CPU usage on the Performance tab of Task Manager.
You will that the total CPU usage is about 50%.
See the picture below:
Question: Why the JVM is not making full use of all 4 CPU-Threads? I have no idea.
5. Force Windows to use only 1 CPU-Thread by going to the on the Processes tab of Task Manager. Then right-mouse click on the "java.exe" process, which represents the running MultithreadingCpuRunner.java program. When select "Set Affinity...", you will see the processor affinity options. Uncheck CPU 1, 2, and 3. Then click OK to force Windows to only use CPU-Thread 0 for the execution of MultithreadingCpuRunner.java.
6. Go back to view individual CPU-Thread usage on the Performance tab of Task Manager again. You will see that the running process is being distributed only to the first CPU-Thread:
CPU-Thread 1 - 100% used CPU-Thread 2 - 0% used CPU-Thread 3 - 0% used CPU-Thread 4 - 0% used
This behavior is expected, because we asked the Windows to only use the first CPU-Thread. Of course, the total productivity is dropped almost by half and the average productivity is slowly reaching 885:
Seconds | Productivity per thread | Total | Average ... 321 | 421 472 | 890 | 900 322 | 416 467 | 883 | 900 323 | 453 432 | 886 | 900 324 | 470 417 | 881 | 900 325 | 427 466 | 885 | 900 ...
7. Go back to the Process tab and force Windows to use the first 2 CPU-Threads. Windows will listen to us and allocate both CPU-Threads to MultithreadingCpuRunner.java:
CPU-Thread 1 - 100% used CPU-Thread 2 - 100% used CPU-Thread 3 - 0% used CPU-Thread 4 - 0% used
However, the performance is not back to the same level when CPU-Thread 1 and 3 was 100% used. The total productivity is around 985 and the average productivity is slowly reaching the same number:
Seconds | Productivity per thread | Total | Average ... 711 | 493 495 | 985 | 993 712 | 492 494 | 986 | 993 713 | 493 491 | 984 | 993 714 | 491 494 | 985 | 993 715 | 494 495 | 985 | 993 ...
The answer is in the design of the 2-CPU-4-Thread architecture. My guess is that CPU-Thread 1 and 3 represent 2 real CPUs. CPU-Thread 2 and 4 are just secondary processing threads added to each real CPUs.
If we use the average productivity number from the CPU-Thread 1 execution, 885, and from the CPU-Thread 1 and 2, 985, we can estimate the execution power of CPU-Thread 2 as (985-885)/885 = 11% of CPU-Thread 1. If CPU-Thread 3 and 4 are behave in the same way CPU-Thread 1 and 2, we can estimate that my 2-CPU-4-Thread architecture is giving me only about 2.22 CPUs in terms of execution power:
CPU-Thread #1 - 100% CPU CPU-Thread #2 - 11% CPU, if used together with #1 CPU-Thread #3 - 100% CPU CPU-Thread #4 - 11% CPU, if used together with #3
Note that this unequal execution power on each CPU-Thread will cause Windows total CPU usage report inaccurate. For example, when MultithreadingCpuRunner.java in running on CPU-Thread 1 and 3 with 100% usage on each of them, Windows reports 50% usage of total CPU. But in reality, 90% (200 out of 222) of the total CPU power is used.
Last update: 2014.
Table of Contents
Downloading and Installing JDK 1.8.0 on Windows
Downloading and Installing JDK 1.7.0 on Windows
java.lang.Runtime Class - The JVM Instance
java.lang.System Class - The Operating System
ClassLoader Class - Class Loaders
Class Class - Class Reflections
JRockit JVM 28.2.7 by Oracle Corporation
Memory Management and Garbage Collectors
JVM Stack, Frame and Stack Overflow
Thread Testing Program and Result
►CPU Impact of Multi-Thread Applications
PrimeCalculator.java - CPU Intensive Process
CPU Intensive Process - 1 Thread per CPU
Single Thread Process on 2-CPU-4-Thread Machine
►Two-Thread Process on 2-CPU-4-Thread Machine
Multi-Thread Process on 2-CPU-4-Thread Machine
Multi-Thread Process Slows Down System Response Time
Multi-Thread Process Running on JRockit JVM
I/O Impact of Multi-Thread Applications
Micro Benchmark Runner and JVM Options
Micro Benchmark Tests on "int" Operations
Micro Benchmark Tests on "long" Operations
Micro Benchmark Tests in JIT Compilation Mode