Java Tutorials - Herong's Tutorial Examples - v8.22, by Herong Yang
CPU Execution Time Shared by Multiple Threads
This section provides a tutorial example on how to measuring CPU execution time allocated to threads with different setPriorities.
Question: If there are 5 application programs running on a single CPU system, and there are 5 threads running in each of the 5 running application programs, how all the 25 threads get executed simultaneously on that one CPU?
The answer is that those 25 threads do not get executed simultaneous at any given instance of time. They get executed one at a time in turns to share the processing time of the single CPU. The following simple example illustrates how the processing time can be shared by multiple threads.
Assuming that:
If we let the system runs for 0.1 second, each application program will be executed 20 times for a total time of 20 milliseconds, and each thread will be executed 400 times for a total time of 4 milliseconds. So, for a fraction of a second (1/10 second), all threads in all applications programs will be executed for about 4 milliseconds. This will make you feel like all threads get executed simultaneously.
Now let's use the following program to see how each thread is getting its share of the CPU time:
/* RacingThread.java * Copyright (c) HerongYang.com. All Rights Reserved. */ import java.util.*; class RacingThread extends Thread { private static final long s_maxi = 1000000; // maximum # of steps private static final int t_maxi = 5; // maximum # of threads private static long[] t_done = new long[t_maxi]; private static int t_last = -1; // index of the last thread private int t_indx; private static int n_prime = 0; public static void main(String[] a) { System.out.println("Priority: (min, norm, max) = ("+ Thread.MIN_PRIORITY+", "+ Thread.NORM_PRIORITY+", "+ Thread.MAX_PRIORITY+")"); long start_time = new Date().getTime(); for (int i=0; i<t_maxi; i++) { RacingThread t = new RacingThread(); if (i==0) t.setPriority(Thread.MIN_PRIORITY); else if (i==1) t.setPriority(Thread.NORM_PRIORITY); else t.setPriority(Thread.MAX_PRIORITY); t.start(); } System.out.print("Threads: "); for (int i=0; i<t_maxi; i++) { System.out.print(i+" "); } System.out.print("Time"); while(true) { try { sleep(100); } catch (InterruptedException e) { System.out.println("Interrupted."); } System.out.print("\n Steps: "); for (int i=0; i<t_maxi; i++) { System.out.print(t_done[i]+" "); } System.out.print((new Date()).getTime()-start_time); } } public RacingThread() { t_last++; t_indx = t_last; t_done[t_indx] = 0; } public void run() { for (long s=0; s<s_maxi; s++) { int n = 0; for (int i=3; i<100; i++) {// keep it busy for some time boolean is_prime = true; for (int j=2; j<i; j++) { is_prime = (i%j>0); if (!is_prime) break; } if (is_prime) n++; } n_prime = n; t_done[t_indx] = s; } } }
Notes on the program design:
See the next section for execution output of this program.
Table of Contents
Execution Process, Entry Point, Input and Output
Primitive Data Types and Literals
Bits, Bytes, Bitwise and Shift Operations
Managing Bit Strings in Byte Arrays
Reference Data Types and Variables
StringBuffer - The String Buffer Class
System Properties and Runtime Object Methods
Generic Classes and Parameterized Types
Generic Methods and Type Inference
Lambda Expressions and Method References
Java Modules - Java Package Aggregation
►Execution Threads and Multi-Threading Java Programs
What Are Processes and Threads
The "Thread" Class - Creating Thread Objects With Thread Sub Classes
The "Runnable" Interface - Creating Thread Objects with Runnable Objects
►CPU Execution Time Shared by Multiple Threads
CPU Execution Time Shared by Multiple Threads - Test Output
Application Data Shared by Multiple Threads
Application Data Shared by Multiple Threads - Test Results
interrupt() - Method to Terminate Thread
ThreadGroup Class and "system" ThreadGroup Tree
Synchronization Technique and Synchronized Code Blocks
Deadlock Condition Example Programs
Garbage Collection and the gc() Method
Assert Statements and -ea" Option