JVM Tutorials - Herong's Tutorial Examples - Version 4.23, by Dr. Herong Yang
PrimeCalculator.java - CPU Intensive Process
This section describes a thread productivity testing programs, PrimeCalculator.java, that repeats a work unit of finding all prime numbers in a given integer range. It is a good example of CPU intensive process.
Many Java applications are using the multithreading technology to run multiple processes of the same type concurrently instead of running them sequentially. Designers of those applications believe that multithreading will help to get more work done within the same time period.
In order to understand the impact on overall productivity of multithreading technology, let's start with a sample called PrimeCalculator.java to perform a series of work in a process. The unit of work is defined as find all prime numbers with a given integer range. Here is the source code of PrimeCalculator.java:
/* PrimeCalculator.java * Copyright (c) 2014, HerongYang.com, All Rights Reserved. */ public class PrimeCalculator extends Thread { public long longMax; // The maximum integer range to seach public long primeMax; // The maximum prime found public long primeCount; // The number of primes found public long jobCount; // The number of jobs done so far public static void main(String[] a) { long max = 1000; // Default integer range to search long interval = 1; // Default monitoring interval in seconds if (a.length>0) max = Long.parseLong(a[0]); if (a.length>1) interval = Long.parseLong(a[1]); System.out.println("Integer range to search: "+max); PrimeCalculator t = new PrimeCalculator(max); t.start(); int i = 0; long startTime = System.currentTimeMillis(); System.out.println( "Time, Count, Productivity, PrimeCount, PrimeMax"); while (true) { try { sleep(interval*1000); } catch (InterruptedException e) { System.out.println("Monitor interrupted."); } long curTime = System.currentTimeMillis(); long duration = (curTime - startTime)/1000; long productivity = t.jobCount/duration; System.out.println(duration+", "+t.jobCount +", "+productivity+", "+t.primeCount+", "+t.primeMax); } } public PrimeCalculator(long max) { longMax = max; primeCount = 0; primeMax = 0; } public void run() { jobCount = 0; while (true) { long count = 0; long max = 0; for (long i=3; i<=longMax; i++) { boolean isPrime = true; for (long j=2; j<=i/2 && isPrime; j++) { isPrime = i % j > 0; } if (isPrime) { count++; max = i; } } primeCount = count; primeMax = max; jobCount++; } } }
Some notes on PrimeCalculator.java:
PrimeCalculator.java seems to be working correctly, as you can see from the following test results:
C:\>"\Program Files\java\jdk1.7.0_45\bin\javac" PrimeCalculator.java C:\>"\Program Files\Java\jdk1.7.0_45\bin\java" PrimeCalculator 10 Integer range to search: 10 Time, Count, Productivity, PrimeCount, PrimeMax 1, 2157548, 2157151, 3, 7 2, 4424402, 2211945, 3, 7 3, 6682344, 2227439, 3, 7 4, 8939961, 2234982, 3, 7 5, 11208084, 2241519, 3, 7 ... C:\>"\Program Files\Java\jdk1.7.0_45\bin\java" PrimeCalculator 100 Integer range to search: 100 Time, Count, Productivity, PrimeCount, PrimeMax 1, 47040, 47039, 24, 97 2, 94461, 47229, 24, 97 3, 142121, 47373, 24, 97 4, 190369, 47592, 24, 97 5, 238784, 47756, 24, 97 ... C:\>"\Program Files\Java\jdk1.7.0_45\bin\java" PrimeCalculator 1000 Integer range to search: 1000 Time, Count, Productivity, PrimeCount, PrimeMax 1, 768, 768, 167, 997 2, 1602, 801, 167, 997 3, 2435, 811, 167, 997 4, 3266, 816, 167, 997 5, 4090, 818, 167, 997 6, 4922, 820, 167, 997 7, 5758, 822, 167, 997 8, 6588, 823, 167, 997 9, 7421, 824, 167, 997 10, 8254, 825, 167, 997 ...
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