Java multithreading (three) thread life cycle and priority

Source: Internet
Author: User
Tags java se

Java Multithreading (three) thread life cycle and prioritythe life cycle of a thread

  the life cycle of a thread: the process of creating a thread from creation to extinction.

For example, represents the various states in the thread life cycle:

  

  The life cycle of a thread can be divided into four states:

1. Create a status:

The thread is created when a new thread object is created using the new operator .

the thread in the creation state is simply an empty thread object , and the system does not allocate resources for it.

  

2. Operational Status:

The start () method of the execution thread assigns the required system resources to the thread, schedules it to run, and invokes the thread body -Run () method , This causes the thread to be in a running state (Runnable).

This state is not in the running state (Running) because the thread may not actually be running .

  

3. Non-operational status:

  When the following events occur, the running thread goes into a non-operational state :

Called The Sleep () method ;

The thread calls the Wait () method to wait for the specific condition to be satisfied;

Thread input/output blocking .

  return to operational status :

A thread that is asleep after the specified time passes ;

  if the thread is waiting for a condition , the other object must notify the waiting-thread condition through the notify () or Notifyall () method ;

If the thread is blocked by the input output, wait for the input output to complete .

  

4. Extinction status :

When the thread's run () method finishes executing , the thread dies naturally .

priority of the thread

  1. Priority and settings for threads

Threads are prioritized in order to facilitate system-to-thread scheduling in multithreaded environments, with high priority threads executing.

The priority settings for a thread follow these guidelines :

When a thread is created, the child inherits the parent's priority.

After the thread is created, the priority can be changed by calling the SetPriority () method.

The priority of a thread is a positive integer between 1-10.

  1-min_priority

  10-max_priority

  5-norm_priority

If nothing is set, the default value is 5.

  However, you cannot rely on thread precedence to determine the order in which threads are executed.

  2. Scheduling Policies for Threads

The thread scheduler chooses the highest-priority thread to run. However, the run of the thread is terminated if the following conditions occur:

The yield () method is called in the thread body, giving up the use of the CPU.

The sleep () method is called in the thread body to put the thread into sleep .

The thread is blocked because of an I/O operation .

  another higher-priority thread appears .

In a system that supports time slices, the time slice of the thread runs out .

Resources

Santhiya Garden Zhang Long teacher Java SE Series video tutorial.

Source: http://www.cnblogs.com/mengdd/archive/2013/02/16/2913649.html

Java multithreading (three) thread life cycle and priority

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.