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