Non-threading approach to Java multithreaded Programming basics

Source: Internet
Author: User
Tags mutex thread ticket

[Wait (), notify ()/notityall () method]

There are a lot of things to explain about these two methods. There may be a lot of places in the following instructions that you can't understand at once, but even if you don't fully understand it after this section, you must go back and remember the following two words:

[Wait (), notify ()/notityall () method is the normal object method (implemented in the object superclass), not the method of the thread object]

[Wait (), notify ()/notityall () method can only be invoked in the synchronization method]

[Mutex control for threads]

When multiple threads manipulate an object at the same time, the operation of one thread on that object may change its state, which affects the true result of another thread on that object.

This example we can see in too many documents, like two conductors selling the same ticket at the same time.

1. Thread A in the database to check the deposit ticket, found that ticket C can sell
Class= "left" 2. Thread a accepts the user's booking request and prepares the ticket.
3. Then switch to thread B execution
4. Thread B in the database to check the deposit tickets, found that ticket C can sell
5. Thread B sold the tickets.
6. Switch to thread a execution, thread a sells a ticket that has been sold

Therefore, a mechanism is needed to manage the occurrence of such problems, and when a thread is executing an integral part, other threads cannot perform this part concurrently.

A mechanism that can only have one thread executing an execution unit at a given moment is called a mutex control or a shared mutex (mutual exclusion)

In Java, mutex control is implemented using the Synchornized keyword (for the time being, JDK1.5 has developed a new mechanism)

[synchornized keyword]

By declaring a unit as synchornized, you can allow only one thread at a time to manipulate the method.

Some people say that synchornized is a lock, in fact it has a lock, but who is the lock, lock who, this is a very complex problem.

Each object has only one monitor lock (monitor lock), which can only be acquired by one thread at a time. When a thread acquires this lock, other threads can only wait for the thread to release the lock before it can be retrieved.

So what exactly is the synchornized keyword locked?

For a synchronization block, synchornized gets the object lock in the parameter:

synchornized(obj){
 //...............
 }

When a thread executes here, it first acquires the lock of this instance of obj and waits only if the thread is not fetched. If more than one thread executes here, only one thread acquires the lock for obj and then executes the statement in {}, so the Obj object has a different scope and a different control program.

If:

public void test(){
 Object o = new Object();
 synchornized(obj){
 //...............
 }
 }

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.