Java Multithreaded life cycle

Source: Internet
Author: User

These two days in a look at the threads of things, according to the Cow People's blog summed up some: Each Java program contains at least one thread: the main threads. Other threads are created through the thread constructor or by instantiating classes that inherit the class thread. A running thread is typically created by the operating system, and the thread object is created by JAVAVM, and the new thread does not actually start executing until a thread calls the start () method on the thread object of the new thread. The thread object already exists before its thread actually starts, and its thread exits after it is still present. It is also necessary to note that the run () method is only a common method in a class, and the direct execution is not the same as the normal method; the start () method is different, it first makes a series of work such as creating a thread (initialization), and then the thread scheduler controls the acquisition of the virtual CPU. Running the Run method on the JVM simply feeds the thread into runnable and then calls the run () method, executing the thread body. To sum up: Start (): It is to start a new thread and the new thread will execute the corresponding run () method. Start () cannot be called repeatedly. Run (): Run () is the same as the normal member method and can be called repeatedly. Calling run () alone will execute run () in the current thread and will not start a new thread!

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

Create State
When you create a new thread class object using the use new operator, the thread is in the created state.
The thread in the creation state is just an object, and the system has not allocated resources for it.
Operational Status
Executes the thread's start () method, assigns a system resource to the thread, schedules it to run, and calls its run () method, which puts the thread into a running state.
The operational state does not necessarily indicate running.
non-operational status
When the following events occur, the running thread goes into a non-operational state:
Call the Sleep () method to put the thread into a sleep state
Call the yield () method to let the CPU occupy
The thread calls the wait method to wait for a specific condition to satisfy
Thread IO Blocking
Another higher-priority thread appears
In a system that supports time slices, the time slice of the thread runs out
The Synchronized method is called or the synchronized synchronization code block causes the object to be locked
Yield
public static void Yield ()
Pauses the currently executing thread object and executes other threads.
return to operational status
The sleeping thread is after the specified sleep event
If a thread is waiting for a condition, another object must use the Notify () or Notifyall () method to notify the waiting thread
If the thread is blocked by IO, wait for the IO operation to complete and return to the operational state
The Synchronized method is called and waits for the method execution to complete to unlock
Extinction State

When the Run method finishes executing, it enters the extinction state.


Original link: http://developer.51cto.com/art/200906/130139.htm

Http://www.itzhai.com/java-based-notebook-the-life-cycle-of-thread-priorities-and-thread-status-of-common-method.html


Java Multithreaded life cycle

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.