Java Thread Series (v) synchronized

Source: Internet
Author: User
Java Thread Series (v) synchronizedsynchronized lock re-entry

The keyword synchronized has the ability to re-enter the lock, that is, when using synchronized, when the thread waits for the lock on an object, it can again get the lock on the object when it is requested again. Release the lock when an exception occurs.

Synchronized exception synchronized code block

The use of the synchronized declaration method in some cases is a disadvantage, such as a thread call synchronous method to perform a long time task, then the B thread must wait for a long time to execute, in which case can use the synchronized code block to optimize the code execution time , which is usually said to reduce the granularity of the lock.

Synchronized code block case "Com.herolei._1_2sync. Objectlock "

Synchronized to Chang lock problem "com.herolei._1_2sync. Stringlock "

Note: The lock object changes the problem, when using an object to lock the operation, pay attention to the object itself changes, then hold the lock is different.

Record a little bit every day. Content may not be important, but habits are important!

Java Thread Series (v) synchronized

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.