Article directory
1. Thread Context Switch
因为以下一些原因导致cpu不再执行当前的线程,转换执行另一个线程的代码
- The thread's cpu time slice has run out
- Garbage collection
- There are high priority threads that need to run
- The thread itself calls sleep, yield, wait, join, park, synchronized, lock and other methods
当上下文切换时,需要由操作系统保存当前线程的状态,并恢复另一个线程的状态,Java中对应的概念就是程序计数器(Program Counter Register),它的作用是记住下一条jvm指令的执行地址,是线程私有的
- The status includes the program counter and information of each stack frame in the virtual machine stack, such as local variables, operand stack, return address, etc.
- Frequent occurrence of Context Switch will affect performance