Concurrent programming (II) java thread

Do spring breeze in the afterlife 2022-01-26 18:21:34 阅读数:556

concurrent programming ii java thread

One 、 Creating and running threads

1. Method 1 , Use it directly Thread


2. Method 2 , Use Runnable coordination Thread


java 8 Can be used later Lambda To streamline the code


3. Method 3 ,FutureTask coordination Thread

FutureTask Be able to receive Callable Parameters of type , Used to deal with situations with returned results

Two 、 How to view process threads

1. windows

(1)tasklist Check the process


 (2)taskkill Kill process


2. linux


3. Java


3、 ... and 、 Principle of thread operation

1. Stack and stack frame

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


2. Thread context switch (Thread Context Switch)

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

Four 、 Common methods

1. start And run

(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

2.sleep And yield

2.1 sleep

(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


2.2 yield

(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

2.3 Thread priority

(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 .

2.4 Case study Restricted pair cpu Use

copyright:author[Do spring breeze in the afterlife],Please bring the original link to reprint, thank you.