Do spring breeze in the afterlife 2022-01-26 18:21:34 阅读数:556
java 8 Can be used later Lambda To streamline the code
FutureTask Be able to receive Callable Parameters of type , Used to deal with situations with returned results
（1）tasklist Check the process
（2）taskkill Kill process
Java Virtual machine Stacks（Java Virtual machine stack ）
We all know JVM Zhongyou heap 、 Stack 、 Method area . After each thread starts , The virtual machine will allocate a piece of stack memory .
（1） Each stack consists of multiple stack frames （Frame） form , It corresponds to the memory occupied by each method call .
（2） Each thread can only have one active stack frame , Corresponds to the currently executing method .
Stack frame diagram
Due to the following reasons cpu The current thread is no longer executed , Instead, execute the code of another thread
（1） Thread cpu I ran out of time
（2） Garbage collection
（3） Higher priority threads need to run
（4） The thread called itself sleep、yield、wait、join、park、synchronized、lock Other methods
When context switching occurs , The state of the current thread needs to be saved by the operating system , And restore the state of another thread ,Java The corresponding concept in is program counter , Its function is to remember the next item jvm The execution address of the instruction , Threads are private .
（1） Status includes program counters 、 Information of each stack frame in virtual machine stack , Such as local variables 、 The stack of operands 、 Return address, etc
（2） Frequent context switching will affect performance
（1） Call directly run It is executed in the main thread run, No new thread started
（2） Use start Is to start a new thread , Through new inter thread execution run The code in
（1） call sleep Will cause the current thread to Running Get into Timed Waiting state （ Blocking ）
（2） Other threads can use interrupt Method to interrupt the sleeping thread , At this time sleep Method will throw InterruptedException
（3） After sleep, the thread may not be executed immediately
（4） Suggest using TimeUnit Of sleep Instead of Thread Of sleep For better readability
（1） call yield Will cause the current thread to Running Get into Runnable Ready state , Then schedule to execute other threads.
（2） The specific implementation depends on the task scheduler of the operating system
（1） Thread priority will prompt （hint） The scheduler schedules the thread first , But it's just a hint , The scheduler can ignore it .
（2） If cpu Busy , The thread with higher priority will get more timeslices , but cpu Leisure , Priority has little effect .
copyright：author[Do spring breeze in the afterlife]，Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201261821325557.html