Java carefully selected

Source: Internet
Author: User

1, if there are two threads, a thread A, a thread B will access a lock method, there may be concurrency, but thread B access is frequent, thread A has few access times, ask how to optimize. (then the interviewer said there was an understanding of the overweight and light locks.)

A, competitive resources are not fierce, choose synchronized:

In the case of resource competition is not very intense, synchronized performance is better than Reetrantlock,

However, when the resource competition is very intense , the performance of synchronized will be reduced by dozens of times times , but the performance of Reetrantlock can maintain the normal ;

b, optimization, assuming that threads A and b do not have concurrent access, and if concurrency occurs, it expands to a heavyweight lock:

The core idea of a lightweight lock is that " locked code does not occur concurrently, and if concurrency occurs, it expands into a heavyweight lock ."

Java carefully selected

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.