1. multithreaded programs with respect to the relationship between the speed of single-threaded programs
Amdahl's law of
Suppose the number of processors is N, the ratio of the program must serial (ie not concurrent of) accounted for part of the time-consuming procedures of all time-consuming to p, then the program will be changed to such a multi-threaded program, in theory, we can get the the maximum speed S max is
Smax = 1 / (p + (1 - p) / N);
Setting T (1) single-threaded version running total elapsed time for the program, a multi-threaded run total elapsed time T (N) for the program, then the program is a multithreaded program to the obtained speed S max is
Smax = T(1) / T(N)
N->∞ Smax = 1 / p
2. Set the number of threads of principle
N set CPU number indicates a processor system, N CPU specific values may be obtained by Java code:
int nCPU = Runtime.getRuntime().avaliableProcessors();
For CPU-intensive thread 2.1, the number of threads may be set to N CPU +. 1;
2.2 for I / O intensive thread priority consideration to the number of threads is set to 1, only in a case where a thread is not enough to be the number of threads N * 2 CPU close
2.3 software for processor usage threshold is set
Nthreads = Ncpu * Ucpu * (1 + WT / ST)
N Threads is the number of threads reasonably sized, N CPU is the number of CPU, the U- CPU target CPU usage (0 <the U- CPU <=. 1), the WT (the Wait Time) spent waiting for the program (e.g., waiting for I / O operations the results on long), ST (Service time) is the length of a program executed by a processor when the actual occupancy calculation. In practice, we can use the monitoring data provided jvisualvm calculated value WT / ST of.