Home  >  Article  >  The life cycle of a thread is divided into several parts

The life cycle of a thread is divided into several parts

清浅
清浅Original
2019-04-02 17:41:4949150browse

The life cycle of a thread is divided into five parts: new, ready, running, blocking and death. Since the CPU needs to switch between multiple threads, the thread status will also switch between multiple runs and blocking

The life cycle of a thread is divided into several parts

##[Recommended course: C Language Tutorial

When a thread is created and started, it neither enters the execution state as soon as it starts, nor is it always in execution. state. In the life cycle of a thread, it goes through five states: New, Runnable, Running, Blocked and Dead. Especially after a thread is started, it cannot always "occupy" the CPU to run alone, so the CPU needs to switch between multiple threads, so the thread status will switch between running and blocking multiple times.

Five states of the life cycle

New Thread

When creating an instance of the Thread class (object ), this thread enters the new state (not started).

For example:

Thread  t1=new Thread();

Ready (runnable)

The thread has been started and is waiting to be assigned to the CPU time slice, which means that at this time The thread is waiting in the ready queue to get CPU resources

For example:

t1.start();

Running

The thread has obtained CPU resources and is executing the task (run () method), at this time, unless this thread automatically gives up CPU resources or a thread with a higher priority enters, the thread will run until the end.

Blocked

For some reason, the running thread gives up the CPU and suspends its own execution, that is, it enters the blocking state.

Sleeping: Use the sleep(long t) method to put the thread into sleep mode. A sleeping thread can enter the ready state after the specified time has elapsed.

Waiting: Call the wait() method. (Call the motivate() method to return to the ready state)

Blocked by another thread: call the suspend() method. (Call the resume() method to resume)

Death (dead)

When the thread completes execution or is killed by other threads, the thread enters the death state. At this time, the thread It is no longer possible to enter the ready state and wait for execution.

Natural termination: Terminate after running the run() method normally

Abnormal termination: Call the stop() method to stop a thread from running

Summary: The above is the summary of this article All content, I hope it will be helpful to everyone

The above is the detailed content of The life cycle of a thread is divided into several parts. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn