Interview question:
Do you know anything about locks in java? What kinds of locks are there? What's the difference between them? Tell me.
(Learning video sharing:java video tutorial)
Answer:
It can be roughly divided into the following points: what is a lock, what is it used for, what types of locks are there, and the differences between locks.
1:What is a lock and what is it used for?
Locks are mainly used to achieve synchronization of resource sharing. Only after acquiring the lock can you access the synchronization code, otherwise wait for other threads to finish using the lock to release the lock.
2:What kind of locks are there?
The main focus here is: synchronize and Lock (because the interview is asking about these two).
synchronize: It can be placed in front of the method; it can also be placed in front of the code block, but the locked object needs to be specified. Usually used together with wait, notify, notifyAll. wait: Release the object lock held and release the CPU. sleep: releases the CPU, but does not release the occupied object lock.
notify: Wake up a thread in the waiting queue so that it can obtain the lock for access.
notifyAll: Wake up all threads waiting for the object lock in the waiting queue and let them compete to obtain the lock.
Lock: Has the same semantics as synchronize, but adds some other features, such asInterrupt lock waiting and timed lock waiting, So you can use lock instead of synchronize. The methods provided are:
(Recommendations for more related interview questions:java interview questions and answers)
lock(): Obtain the lock in a blocking manner. It will wait until it is not acquired and will not be interrupted.
tryLock(): Get it. If it is obtained, it will return true. If it is not obtained, it will return false.
tryLock(long timeout,TimeUnit unit): Returns true if obtained, waits for the given time if not obtained, and has not been obtained yet Return false when it arrives.
##lockInterruptibly(): Similar to lock, but if the lock is not acquired, it will enter the dormant state until the lock is acquired or the current thread is blocked. The thread is interrupted.
Three:What is the difference between the two?
Inconsistent performance: When resource competition is motivated, lock performance will be better than synchronize. When competition is not motivated, synchronize performance will be better than lock.
The lock mechanism is different: synchronize is implemented at the JVM level, and the system will monitor whether the lock is released or not. The lock is implemented in code and needs to be released manually, in the finally block. Locks can be acquired in a non-blocking manner.
The usage is different: synchronize can be used on code blocks and methods. Lock is implemented through code and has more precise thread semantics.
Related recommendations:java introductory tutorial
The above is the detailed content of java interview-lock. For more information, please follow other related articles on the PHP Chinese website!