Core java-Multithreading-The life cycle of a thread

Source: Internet
Author: User
Tags thread class

0. Let's take a look at what the process is before we introduce the thread.

Process is the mother of the thread, if you read the operating system in a college computer course will not be unfamiliar.

The so-called process, which is a computer program on a data set on an activity, is the system of resource allocation and scheduling of the basic unit, is the basis of the operating system structure.

Is it a big pile, or is it not simple enough?

Let's say a word: A process is an instance of a program that is executing.

Memory layout of the process

Logically a process can be divided into the following sections (segments):

* Text: Instructions for the program

* Data: Static variables used by the program

* Heap: Programs can dynamically allocate additional memory from this area

* Stack: A piece of memory that is returned with a function call to allocate storage space for local variables and function call link information

Well, about the process first here, and then go into the contents of the operating system, this is not the focus of this blog post.

1. What is a thread?

A thread is a child task that can be executed independently in a process.

A process can contain multiple threads, and a thread in the same process shares the resources owned by the process, such as memory space and file handles.

Therefore, the process is a thread container, we use multithreading to carry out the concurrent design of the program, because the cost of scheduling is much smaller.

2. Thread Life cycle

Threads in Java have the following states:

1) New (new thread)

You can create a thread with the new operator

Eg:new Thread (R)

The thread created with the above statement is in a new state.

Note: When a thread is in a new state, the program has not started running the code in the thread, and there is a little work to do before our thread can run.

2) RUNNABLE (can be run)

When can the thread run, that is, into the runnable state?

The answer is: Once the Start method is called, the thread becomes runnable .

Note here: a runnable thread does not mean that the thread must be running, this should be clear!!

It is also possible that it is not running, whether the thread is running or not, and how much time the operating system provides for that thread.

Once a thread starts running, it does not need to remain running at all times, and in fact, the thread sometimes needs to be interrupted during the run to

In order for other threads to get the chance to run.

3) BLOCKED (thread is blocked)

When a thread is blocked, other threads are given the opportunity to run the operation.

So how do you initiate a blocking operation?

The scenario is as follows:

A thread initiates a blocking I/O (such as a file read/write/block socket read/write), or attempts to obtain a lock held by another thread.

4) Waiting (thread wait)

When a thread executes certain methods, it waits.

These methods include:

Object.wait (), Thread.Join (), Locksupport.park ()

In contrast, we can also make the thread continue from the waiting state back to the operational (Runnable) state, by the following methods:

Object.notify (), Object.notifyall (), Locksupport.unpark (thread)

5) Time_waiting (limited time wait)

This state is similar to waiting, except that the thread is not waiting indefinitely, but a limited wait, and when the other thread does not expect the action within the specified time, the thread state

Automatically to operational (RUNNABLE).

6) TERMINATED (thread end)

When the thread executes at the end, it is in state, and this is when the thread's life cycle terminates.

There are two cases of thread termination:

One is normal end return

The other is because the exception was previously terminated

3. Key code to start a thread in Java

First, we have to know that the Runnable interface

The Runnable interface has a run () method that is used to start the thread class for us.

public interface Runnable

{

void run ();

}

What we're going to do is write a class that implements the Runnable interface, as follows:

Then create an instance for the Runnable class

New Myrunnable ();

Next, the thread object is created by passing the Runnable class to the constructor of the thread class

New Thread (R);

Finally, start the thread

T.start ();

Core java-Multithreading-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.