The life cycle of a thread

Source: Internet
Author: User
Tags mutex thread class

1. Thread Life cycle
A thread is a process that executes dynamically, and it also has a process from generation to death.

(1) Five states of the life cycle

New (new Thread)
When an instance (object) of the thread class is created, the thread enters a new state (not started).
For example: Thread t1=new thread ();

Ready (runnable)
The thread has been started and is waiting to be allocated to the CPU time slice, which means that the thread is waiting in the ready queue to get CPU resources. For example:T1.start ();

Run (running)
The thread obtains the CPU resources that are executing the task (the Run () method), at which point the thread will run until the end, unless the thread automatically discards the CPU resources or has a higher priority thread entry.

Death (dead)
When a thread executes or is killed by another thread, the thread goes into a dead state, and the thread is no longer ready to wait for execution.

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

Abort: Call the Stop () method to let a thread terminate the run

Clogging (blocked)
For some reason, the running thread is getting out of the CPU and pausing its own execution, which goes into a blocked state.

Sleeping: Use the sleep (Long T) method to put the thread into sleep mode. A sleeping thread can go into a ready state at the specified time.

Waiting: Call the Wait () method. (Call Motify () method back to Ready state)

Blocked by another thread: Call the Suspend () method. (Call Resume () method recovery)

2. Common methods

void run () The method that must be implemented when creating subclasses of this class

void Start () method to open thread

static void sleep (Long T) frees the CPU for execution and does not release the lock

static void sleep (Long millis,int Nanos)

final void Wait () releases the execution of the CPU, releasing the lock

Final void Notify ()

static void Yied () can temporarily pause the current thread (let the thread release the resource)

3. (1) End threading principle: Just let the Run method end. In the Run method, the loop structure is usually defined, so as long as the loop is controlled

(2) The method----can be done in the form of a Boolean tag, as long as the tag is changed in a certain situation, so that the loop stops to let the thread end

(3) Public final void Join ()//Let the thread join execution, the thread that executes a one-thread join method is frozen, waiting for a thread to finish executing, and it will revert to the operational state

4. Critical resources: Data shared between multiple threads is called a critical resource

(1) Mutual exclusion lock

A. Each object corresponds to a tag that can be called a "mutex", which is used to guarantee that only one thread can access the object at any one time.

The B.java object can be shared by more than one thread by default, and only starts the mutex mechanism when needed to become a private object.

C. keyword synchronized to associate with an object's mutex

D. When an object is decorated with synchronized, it indicates that the object has initiated a "mutex" mechanism, which can only be accessed by one thread at any one time, even if the thread is blocked, the object's locked state will not be lifted, and the object cannot be accessed by any other thread.

The life cycle of a thread

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.