Java Concurrent Programming Series (i) Basic concepts

Source: Internet
Author: User
Tags thread class

1. Thread Status graph:

5 different states:

1. Newly created (new)

2. Ready (Runnable)

3. Operation (Running)

4. Blocking (Blocked)

    • Wait blocking: Call the Wait () method

    • Synchronous blocking: The synchronized () method was called or the synchronized code block was executed, but no synchronization lock was obtained for the object.

    • Other blocking: Call the Sleep () or join () method

5. Death (Dead)

Related methods: 1.run ():

is the only abstract method in the Runnable interface that can be called repeatedly. Call the Run method directly, and do not start a new thread, the program is still only the main thread of the threads, its program execution path is only one, or to execute sequentially, or wait for the Run method body to complete before you can continue to execute the following code, so that it does not achieve the purpose of multi-threading. Summary: The Run method is just a normal method call to thread, or it executes in the main thread.

2.start ():

Starts a new thread. Starting with the start () method to start a new thread, so that the thread is in a ready (operational) State, once the CPU time slice, start executing the corresponding thread of the run () method, where the method run () is called the thread body, it contains the content of this thread to execute, the Run method runs the end, This thread terminates. Start () cannot be called repeatedly. Start the thread with the Start method, and really implement multi-threaded operation, that is, no need to wait for a thread of the Run Method body code execution, proceed directly to execute the following code.

The difference between 3.start () and run ():
Characteristics Start () Run ()
Start a new thread Yes No
Repeated calls No Yes
Whether to implement multithreading Is Whether
4.wait ()/notify ()/notifyall ()

These three are all methods of the object () class:

Wait (): Leave the current thread in the wait (blocked, pending) state, until the other thread calls the Notify () method of this object or the Notifyall () method, and the current thread is awakened (into ready state). Notify (): Wakes a single thread waiting on this object monitor. Notifyall (): Wakes all the threads waiting on this object monitor.

The Wait (), notify () functions in object, like synchronized, operate on the "object's synchronous lock."

Wait () causes the "current thread" to be waiting because the thread goes into a wait state, so the thread should free its lock-held "sync lock", otherwise the other thread will not get the "sync lock" to run!
OK, after the thread calls wait (), it releases the "Sync Lock" held by the lock, and, according to the previous introduction, we know that the waiting thread can be awakened by notify () or Notifyall (). Now, consider a question: Notify () is based on what wakes the waiting thread? Or, what is the connection between wait () waiting thread and notify ()? The answer is: according to the "synchronization lock of the object".

The thread responsible for waking the waiting thread (what we call a " wake-up Thread ") is only getting "the object's Sync lock" ( where the sync lock must be the same as the waiting thread's Sync lock ), and calls notify () or Notifyall () method to wake the waiting thread. Although the wait thread is awakened, it cannot be executed immediately, because the wake-up thread also holds "the object's synchronization lock." You must wait until the wake-up thread frees the "object's synchronization lock," and wait for the thread to get to the "object's synchronization lock" and continue to run.

In summary, notify (), wait () depends on "sync lock", and "Sync Lock" is Object lock held, and each object has and only one! This is why notify (), wait () functions are defined in the object class, not in the thread class.

5.yield ():

Yield () is the function of concession. It allows the current thread to enter the "Ready state" from the "running state", allowing other waiting threads with the same priority to get execution, but there is no guarantee that other threads with the same priority will be able to gain execution after the current thread calls yield (), or that the current thread is entering the " Run status "continue running!

The difference between yield () and wait ():

Wait () is to allow a thread to enter the "waiting (blocking) state" from the "running state" and not yield () to allow the thread to go from "Running state" to "ready state".
Wait () is a thread that frees the synchronization lock on the object it holds, and the yield () method does not release the lock.

6.sleep ():

Sleep () is defined in Thread.java.
The function of sleep () is to let the current thread hibernate, that is, the current thread goes from "Running state" to "hibernate (blocked) state". Sleep () Specifies the sleep time at which the thread sleeps longer than/equal to the sleep time, and when it is woken up, it is changed from "blocked state" to "ready state", thus waiting for the CPU to schedule execution.

The difference between sleep () and wait ():

The role of Wait () is to allow the current thread to enter the waiting (blocking) state by the "running state" and also release the synchronization lock. The function of sleep () is to let the current thread go into hibernation (blocked) state by the "Running state".
However, wait () frees the object's synchronization lock, and sleep () does not release the lock.

7.join ():

Join () causes a child thread to join a main thread and let the main thread wait for the child thread to end before it can continue.

8.interrupt (): Interrupts a thread

Usage of 2.synchronized Keywords

First : When a thread accesses the "synchronized method" or "Synchronized code block" of "an object", the other thread has the " synchronized method" or " Access to the synchronized code block will be blocked.
Second: When a thread accesses the "synchronized method" or "Synchronized code block" of an object, other threads can still access the non-synchronized code block of the object .
Third: When a thread accesses the "synchronized method" or "Synchronized code block" of "an object", other threads access to other "synchronized methods" or "synchronized code blocks" of the object will be blocked.

Mainly divided into:

1.synchronized method

2.synchronized BLOCK: Synchronized (this): Gets the current object's synchronization lock/synchronized (obj): Gets the object lock for a particular object

Java Concurrent Programming Series (i) Basic concepts

Related Article

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.