Java-Preliminary Understanding-the 14th chapter-Inter-threading communication-multi-producer multi-consumer problem-jdk1.5 new features-condition

Source: Internet
Author: User

One.

Now wait method and Notifyall method, we compare tangled. Why do we say that the locks in the previous sync are custom, such as This,obj, and now the lock is our own new one and is based on the lock object after 1.5.

When this and obj were previously written, the Wait method and the Notifyall method are all in this lock operation, now the lock is gone, now the lock is lock. Can this wait method still operate this? Specifically, the wait method cannot manipulate this locked thread because the thread's lock has changed. Should use lock lock method, some people directly changed to lock.wait ();

That's right, but there's a bit of a change in the way things are, and the upgrades are different from what they used to be. Using a locked wait is definitely the right idea, because we know that the way the monitor is done is based on the thread that the monitor is monitoring. Look at the following sentences,

Now, what do you mean by the above?

In the previous synchronization, for synchronization, the lock can only have a set of monitor methods. What do you mean? Assuming that this lock is this one, this is the only group Wait,notify,notifyall this method. Each lock is bound to a group, because each lock is a subclass of the object class, and it takes such a set of methods directly. The r.wait,r we said before is the lock, it can only have such a group of wait,notify. And in the new version, a little bit changed,

condition encapsulates the method in object to encapsulate the set of monitors (Wait,notify,notifyall) individually as a condition object, while a lock lock can hang multiple conditon, Each condition has a set of monitors,

In, there is an obj. It has only one set of Wait,notify,notifyall methods, because there is only one object, only one group. These three methods can be used to operate the lock. You have only one lock on the sync function, so once the lock becomes an object, it can hang multiple locks, which can encapsulate the monitor method into a Conditon object.

What is condition?

Conditon object, there is a set of methods, and this Conditon object has many groups, they belong to a lock

In other words, these conditon methods can all work on the thread of the lock.

Previously, a lock had only one set of methods (Wait,notify,notifyall), and now a lock can hang multiple. Before the time, object is a lock, is also an object, which encapsulates a lock and a set of methods, now is to separate the locks and methods, locks with lock to represent, and a set of methods with condition encapsulated into objects, method objects can have a number of simultaneous hanging in a lock.

What's the relationship between condition and lock?

Shown in the Newconditon () method, we can always get a monitor method instance.

In the condition class method, the Wait,notify,notifyall three methods are re-stated. The name has changed and the function has not changed.

Await,signal,signalall three methods.

Previously, in the object class, there was a set of Wait,notify,notifyall methods. Also create a custom class demo, as a lock, the new demo is the lock object. This demo class inherits the object class, and it has such a set of methods. To place the lock object in a synchronization function or in a synchronous code block.

Now, we encapsulate the method of the action monitor in the object class into a condition object. The locks and methods are packaged together and are now packaged into objects separately.

As you can see, a lock lock allows you to build multiple Conditon methods. A lock has two sets of monitors. There are three ways to talk about it in each of these monitors.

The purpose of lock is to replace synchronization, and the purpose of Conditon is to replace the method in object.

The monitor must monitor the locked thread before it can.

Now come back and rewrite the original code,

Originally written in accordance with the Object.wati (), lock.wait () to be modified.

The Conditon is a monitor that locks the lock, and the monitor will operate on the lock-locked thread.

The program is restored to the earliest appearance.

Java-Preliminary Understanding-the 14th chapter-Inter-threading communication-multi-producer multi-consumer problem-jdk1.5 new features-condition

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.