Java Multithreaded series 2--multi-Threading life cycle and production consumer model

Source: Internet
Author: User

One, the life cycle of the thread and five kinds of basic states

For the life cycle of the threads in Java, first look at this more classic diagram:


Basically includes all the important knowledge points of multithreading in Java. Mastered the knowledge points in Java, multithreading is basically mastered. Mainly include:

Java threads have v basic state

new State (new): when the thread object pair is created, it enters the new state, such as thread t = new MyThread ();

ready State (Runnable): when the Start () method (T.start ()) of the thread object is called, the thread enters the ready state. A thread that is in a ready state simply means that the thread is ready to wait for CPU scheduling to execute, not to say that the T.start () thread executes immediately;

Run State (Running): When the CPU starts scheduling a thread in the ready state, the thread is actually executing, which is going into the running state. Note: The ready state is the only entry into the running state, that is, the thread must first be in a ready state if it wants to execute in a running state;

blocking State (Blocked): A thread that is in the running state temporarily abandons the use of the CPU for some reason, stops execution, and then enters a blocking state until it enters the ready state, and is then given the opportunity to be called again by the CPU to enter the running state. Depending on the cause of the blockage, the blocking state can be divided into three different types:

1. Wait for blocking: the thread in the running state executes the wait () method, which causes the thread to enter the waiting blocking state;

2. Synchronization blocking--the thread acquires the synchronized synchronization lock failure (because the lock is occupied by another thread), it will enter the synchronization blocking state;

3. Other blocking-the thread will go into a blocking state by calling the thread's sleep () or join () or making an I/O request. When the sleep () state times out, join () waits for the thread to terminate or time out, or the I/O process finishes, the thread is re-entered in a ready state.

dead State (Dead): The thread finishes executing or exits the run () method because of an exception, and the thread ends the life cycle.

Second, production-consumer model

For multi-threaded routines, the producer and consumer models are the most classic.

In fact, it should be "producer-consumer-warehousing" model.

For this model, you should be clear about the points:

1, the producer only in the storage is not full time production, warehouse full stop production.

2, consumers only in storage products can be consumed, Cang is waiting.

3, when the consumer found that the storage of products can be consumed when the producers will be informed of production.

4, producers in the production of consumable products, should inform the waiting consumers to spend.

The following queue is used to implement the production consumer model


Producers:


Consumers


Production consumption model:


Operation Result:

From the results can be seen first into the queue before being consumed ,

You should notice that there is a keyword synchronized,

When it is used to decorate a method or a block of code, it is guaranteed that at most one thread at a time executes the code.

It has the following characteristics:

First, when two concurrent threads access the same object in the synchronized (this) synchronization code block, only one thread can be executed within a single time. The other thread must wait for the current thread to finish executing the block before it can execute the code block.

Second, however, when a thread accesses one synchronized (this) of an object to synchronize a block of code, another thread can still access the non-synchronized (this) synchronous code block in the object.

Third, in particular, when a thread accesses one synchronized (this) of an object to synchronize a block of code, other threads will block access to all other synchronized (this) synchronization blocks in object.


Java Multithreaded series 2--multi-Threading life cycle and production consumer model

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.