1. Ask

Read and write lock has two features, one fairness, one read and write. How does it work?

2.

To tell you the truth, I also hate this kind of interview source level questions, they are useless, also do not test out the applicant’s actual level. Does knowing the source code mean that technology is good? But bat is always like this. So you just have to adapt. In the words of the interview, screening, understanding the principle of priority. Pick the tall among the short. I think online optimization, solutions to online problems and technical architecture design of the project are the main objectives of the interview.

On the other hand, if you’re dealing with advanced Java, you don’t know this, and it’s almost impossible.

3. A

Fair locking means that locks are allocated in the order requested by the thread; A non-fair lock is one that does not follow exactly the order of request, but allows queue-jumping under certain circumstances.

Fair or not, it is implemented by the underlying Sync, FairSync and NonfairSync, and Sync is an inherited AQS. The only difference between these two locks is that when a fair lock obtains the lock, it must first judge whether there are threads queuing in the waiting queue. If so, the thread corresponding to the fair lock will not attempt to acquire the lock.

4. The extension

Actually, look at the code, but a lot of people don’t. Because of the long line count of this class, many people simply give up. I’ll just post the key points here.

You may be faced with a series of questions:

Q: Is preemptive high or fair high?

A: Not fair lock throughput. In preemptive mode, you don’t have to keep track of his order in the queue, you just open the door and release the gate, and whoever gets it gets it. Fair lock you need to record the queue order. So: Fair locks have lower throughput, but non-fair locks can starve threads.

Q: How do they apply?

The locks we use are unfair locks. So just the fair lock scenario.

If you have sequential requirements, you can use fair locks. If you have to give an example, you could describe a raffle system where locks are acquired in strict order of precedence.

Welcome to pay attention to my B station account

B station account

If the content helps you, welcome everyone to like, favorites + attention

Learning exchange group

Communication group