Strange java. Lang. illegalmonitorstateexception

Source: Internet
Author: User

A piece of code today throws java. Lang. illegalmonitorstateexception. The Code is as follows:

private boolean wait = false;public boolean pleaseWait() {synchronized (this.wait) {if (this.wait == true) {return false;}this.wait =true;try {this.wait.wait();} catch (InterruptedException e) {}return true;}

The explanation of illegalmonitorstateexception on javadoc is:

Thrown to indicate that a thread has attempted to wait on an object's monitor or to handle y other threads waiting on an object's monitor without owning the specified monitor.

It seems obscure, such as object's monitor. In fact, the javadoc of the object. Y () function has the following explanations:

A thread becomes the owner of the object's monitor in one of three ways:
1. By executing a synchronized instance method of that object.
2. By executing the body of a synchronized statement that synchronizes on the object.
3. For objects of type class, by executing a synchronized static method of that class.

To put it bluntly, you must use synchronized syntax to bind the wait/notify object before calling wait () or notify.

The problem is that synchronzied has been used for this. Wait variable in the above Code before this. Wait. Wait () is called (). This exception should not be thrown.

After checking the Internet for a long time, I finally found the answer:

The real problem is that this. Wait is a Boolean variable, and this. Wait performs a value assignment before calling this. Wait. Wait:

this.wait = true;

A boolean variable creates a new object when executing the value assignment statement. To put it simply,This. Wait is not the same object before and after the value assignment statement.

Synchronzied (this. Wait) is bound to the old Boolean object, while this. Wait. Wait () uses the new Boolean object. Because the new Boolean object does not use synchronzied for synchronization, the system throws an illegalmonitorstateexception.

The same tragedy may also occur when this. Wait is of the integer or string type.

A solution is to use the corresponding type in Java. util. Concurrent. Atomic. For example, it should be atomicboolean. The atomicboolean type is used to ensure that no new object is generated for its modifications.

Correct code:

private AtomicBoolean wait = new AtomicBoolean(false);public boolean pleaseWait() {synchronized (this.wait) {if (this.wait.get() == true) {return false;}this.wait.set(true);try {this.wait.wait();} catch (InterruptedException e) {}return true;}}

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.