20165317 Eighth week study summary

Textbook content

  • Program: A piece of static code

  • Process: A dynamic execution process of a program, that is, a complete process from code loading, execution to execution completion.

  • Thread: A smaller execution unit than a process. A process can generate multiple threads during its execution.

  • Some memory units (including code and data) in the process can be shared between threads, and the interruption and recovery of threads can save system overhead.

  • A created thread usually goes through the following four states during its entire life cycle:

New: When an object of the Thread class or its subclasses is declared and created, the new thread object is in the new state.

Running: The thread must call the start() method (a method inherited from the parent class) to notify the JVM, so that the JVM will know that there is a new thread queued for switching. Once it is its turn to enjoy CPU resources, this thread can start its own life cycle independently of the main thread that created it.

Interruption: There are 4 reasons for interruption:

(1) The JVM switches the CPU resources from the current thread to other threads, so that the thread gives up the right to use the CPU and is in an interrupted state.

(2) While the thread is using CPU resources, the sleep(int millsecond) method is executed to make the current thread go to sleep.

(3) The wait() method is executed while the thread is using CPU resources.

(4) While the thread is using CPU resources, it executes an operation and enters the blocking state.

Dead: A thread in a dead state does not have the ability to continue running. The thread frees the entity.

  • Common methods for threads:

start() : The thread calls this method to start the thread and make it queue from the new state to the ready queue. Once it is its turn to enjoy the CPU resources, it can start its own life cycle independently of the thread that created it.

run(): The run() method of the Thread class has the same function and function as the run() method in the Runnable interface. Both are used to define the operations performed after the thread object is scheduled, which are automatically called by the system and cannot be referenced by the user program. Methods.

sleep(int millsecond): A thread with high priority can call the sleep method in its run() method to make itself give up CPU resources and sleep for a period of time.

isAlive(): When the thread is in the "new" state, the thread calls the isAlive() method and returns false. Before the thread's run() method ends, that is, before it enters the dead state, the thread calls the isAlive() method to return true.

currentThread(): This method is a class method in the Thread class and can be called with a class name. This method returns the thread that is currently using CPU resources.

interrupt() : A thread that occupies CPU resources can let the sleeping thread call the interrupt() method to "wake up" itself, that is, cause the sleeping thread to have an InterruptedException exception, thus ending the sleep and re-queuing for CPU resources

code upload

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326637113&siteId=291194637