Study notes (33): Chapter 1 Distributed Foundation of Concurrent Programming-Understanding the Significance and Use of Multithreading (Part 2)

Learn now: https://edu.csdn.net/course/play/29000/424234?utm_source=blogtoedu

1. The life cycle of a thread

Thread startup does not run immediately

 

6 types of java threads: NEW, running state (ready state + running state can be changed according to CPU scheduling), blocking state (WAITING+TIMED_WAITING+BLOCKED), termination state

Operating system level 5 types: running status (ready status + running status can be changed according to CPU scheduling), blocking status (WAITING+TIMED_WAITING+BLOCKED), termination status

Blocking: WAITING, TIMED_WAITING, BLOCKED, IO blocking

How to cause WAITING? :Sleep;wait();join;LockSupport.park

如何导致TIMED_WAITING?:sleep(long);wait(long);join(long);LockSupport.park(long)

How to cause BLOCKED?: Synchronized

Why is the thread not running immediately after it is started, but ready?

Because, after the thread is started, the operating system scheduling algorithm is needed to allocate the CPU. This stage is in the ready state. After the allocation is completed, it enters the running stage

2. Start of thread

new Thread().start() start a thread = "By calling the native start0 C language method, call the method of starting the thread at the JVM level; we need to know that a thread of knowledge is provided by the operating system

new Thread().run() calls the instance method

3. Termination of the thread

3.1. Under what circumstances does the thread terminate?

  (1) The run() method execution ends

(2) Interrupt() is not a method of forced interruption. Only use interrupt() if the run() method cannot end normally; or interrupt the thread in sleep(long)

while(!Thread.currentThread().isInterrupted()){

}

1 "There is also a necessary condition for judging whether the interrupt() method can be used, that is, to see if the InterruptedException is thrown in the code. If the exception is not thrown in the exception handling of catch (InterruptedException e), it is not If the thread is interrupted, it will trigger a thread reset and make interrupted become false. If the caught exception is thrown to report an error, the thread will be interrupted

 

2" Give a similar example?

try{

}catch(InterruptedException e){

e.printStackTrace();//No processing, no interruption

throw new RuntimeException();//Forced to end, it's time to work

Thread.currentThread().interrupt();//Interrupt again; //Don’t go to bed, I don’t know if it will work next

}

During the lunch break, while sleeping (while loop), the alarm clock rings (interrupter() method). If you choose to turn off the alarm clock and do nothing (do not handle the exception), then continue to sleep; if you get up and work immediately (throw an exception, Tell you it’s time to work), then sleep is interrupted; or after the alarm rings, reset the alarm mode and continue to ring (that is, continue to interrupt Thread.currentThread().interrupt() in catch), then you must not sleep well , Will stay awake, but it doesn’t matter whether you start to work

3 "What is reset? How to reset? Why reset?

Reset is to turn the thread into an initialized state, interrupted becomes false;

Method of reset thread.interrupt();

Reasons for resetting: The interruption may be unreasonable. If it is interrupted under unreasonable circumstances without resetting, then the thread cannot proceed. So reset after the interruption, so if no exception is thrown for the interruption (InterruptedException) or Interrupt again, then the code while() can continue, so the ultimate purpose of reset is to prevent accidental killing

4 "interrupt () role?

(1) Set the value of a shared variable to true

  (2) Wake up threads in a blocked state

Guess you like

Origin blog.csdn.net/qq_28500837/article/details/112859744