Java multi-threaded single-process mode

java is a single multi-threaded process model, multiple threads can still take full advantage of multi-core (core) / multi-processor (cpu)

  • Single cpu threads at the same time can only perform a single command, which is a thread
  • Single threads can only be performed in a single cpu thread
  • All threads in the Java JVM process, CPU scheduling is the process of thread
  • Java multi-threading is not due to the number of threads for multiple cpu it is called multi-threaded (single cpu mononuclear do not use Hyper-Threading, you can still achieve multi-threaded Java, but all the threads are running in single-threaded OS's), when the number of threads is greater than the other thread Java cpu number of threads, the operating system uses round-robin (RR) scheduling algorithm, frequent context switching, for concurrent execution
  • By default, Java to create a thread for each operating system will allocate 1M of stack space
  • cpu execute Java programs, which is a fundamental instruction operation is performed after the java code compilation system that can be recognized, cpu instruction execution time is ns level (1.6G cpu to execute an instruction, takes about 0.6ns), and the context switch cpu We need 20000 CPU time periods

      /proc/sys/kernel/thread-max 系统可以生成最大线程数量
    
      /proc/sys/kernel/pid_max 增大,线程数量增大,pid_max有最高值,超过之后不再改变,而且32,64位也不一样

jvm configuration View

  1. There are options to see which flag

     jinfo -flags pid
  2. View jvm value of each configuration

     jinfo -flag ThreadStackSize pid

Set the number of threads

  • Experience: the number of threads the server * 2 + 1
  • The number of threads computationally intensive, the number of processor cores created =
  • If the time-consuming operation io, adjusting the number of threads depending on the circumstances, the number of threads at this time = n * number of processor cores
  • Equal to the number of threads cpu general procedure two to three times the number of threads can be a good use of the cpu, the program not only excessive number of threads does not improve performance, but also because of the frequent switching between threads affected, the specific needs according to the business test thread processing slightly constantly adjust the number of threads, to determine the optimal number of threads in the current system

          查看某个进程的线程栈设置大小
      jinfo -flag ThreadStackSize pid

Process / thread context switch you can use up much CPU?
Tools for Performance Analysis

Guess you like

Origin www.cnblogs.com/shengulong/p/11729847.html