Java thread Synchronization

Source: Internet
Author: User

The recent use of thread synchronization, the idea is not very clear, only remember synchronized and lock, the difference between what is not clear. Take a little space and summarize:

The lock interface basically has all the features of the synchronized

There are also three forms of lock acquisition (interruptible, non-disruptive, and timed) that may vary in their performance characteristics, sequencing guarantees, or other implementation quality.

such as threads A and b are to get the lock of the object o, a thread first obtains the lock of the object o, then B will wait for a to release the lock on O

If you are using synchronized, if a does not release the lock, B will wait until a unlocks the O

If you are using lock, if a does not release the lock, B can wait for a period of time to break the wait and do something else

Speed, in the case of less competition, synchronized faster thanlock, the competition is fierce in the case of lock faster than synchronized.

The synchronized is implemented on the JVM, and if an exception is thrown, the JVM will automatically release the lock.

Lock is a code implementation that requires manual handling of exception release locks



Java thread Synchronization

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.