Java thread Synchronization understanding-in fact, It should be called Java threads queued

Source: Internet
Author: User
Tags object object semaphore

Understanding of thread Synchronization in Java
The true meaning and literal meaning of thread synchronization is exactly the Opposite. The true meaning of thread synchronization is actually "queued": there are several threads to queue, one to operate on shared resources, and not to operate CONCURRENTLY.









Thread sync lock This model looks very Intuitive. however, There is still a serious problem unresolved, where should this sync lock be added? of course, It was added to the shared Resources. Fast-reacting readers are sure to be the first to Answer. yes, If possible, we will certainly try to add the sync lock to the shared resource.
Some more perfect shared resources, such as file system, database system, etc., have provided a relatively perfect synchronization lock Mechanism. We don't have to lock these resources in addition, and these resources have locks on their own. however, in most cases, the shared resources that we access in code are relatively simple shared objects. There's no place in these objects to lock US up. Readers may make suggestions: why not add a new area inside each object, specifically for lock-in?
This design is certainly feasible in theory. The problem is that thread synchronization is not a common situation. If a lock space is created inside all objects because of this small probability event, there will be great space Waste. Outweigh As a result, modern programming languages are designed to add synchronous locks to code Snippets. specifically, The sync lock is added to the code snippet to access shared Resources.
The synchronization lock is added to the code snippet, which solves the problem of space wasting above.
Now let's take a closer look at the thread synchronization model for "synchronous lock plus on code snippets":
first, we've solved the problem of where the sync lock is Added. We have determined that the sync lock is not added to the shared resource but is added to the code snippet that accesses the shared resource. second, the question we're trying to solve is, what kind of lock should we add to the code snippet?
This question is the focus of the Focus. This is our particular concern: access to the same shared resources of different pieces of code, should be added to the same synchronization lock, if the addition of a different synchronization lock, then there is no synchronization function, there is no Point. This means that the synchronization lock itself must also be a shared object between multiple threads.


In this case, you will be curious about what this sync lock is all About. Why is it possible to declare a single Object object as a synchronous lock?
Because it is true that any object reference can be used as a synchronous lock.
We can understand the object reference as the memory address of the objects in the memory allocation System.
so, to make sure that the same sync lock is used between the synchronization snippets, we need to ensure that the Synchronized keyword for these synchronized snippets uses the same object Reference, the same memory address.
...

(1) wait for notification of a semaphore public static final Object signal = new Object (), ... F1 () {synchronized (singal) {///first We want to get this semaphore. This semaphore is also a synchronous lock//only after the successful acquisition of the signal signal and synchronous lock, We can enter this code signal.wait (); This is to give up the Semaphore. This thread wants to enter the signal semaphore (waiting) Queue//poor. Hard to get the signal volume, so be given up//wait until the notification, from the call (waiting) queue to the ready queue//go to the ready queue, a step closer to the CPU core, there is a chance to continue to execute the following Code. It is still necessary to compete with the signal Sync lock in order to actually continue executing the code BELOW. Commiseration Ah. ... } }

(2) notification of a certain semaphore ... f2 () {synchronized (singal) {///first, We also want to get this semaphore. It is also a synchronous lock. Only after the successful acquisition of the signal signal and synchronization lock, We can enter this code signal.notify (); here, we notify signal of a thread in the queue to be Called. If a thread waits for this notification, that thread will go to the ready queue//but this thread still continues to have signal this synchronization lock, this thread still continues to execute//hehe, although this thread kindly notifies other threads,//however, this thread can not be so sharp sense, give up the sync lock// This thread continues to execute the following code ...} }

...
Full Version: http://blog.csdn.net/u012179540/article/details/40685207



Java thread Synchronization understanding-in fact, It should be called Java threads queued

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.