2019.6.25 lock

1. Please describe briefly the similarities and differences between synchronized with java.util.concurrent.locks.Lock of?

Inspection point: Locks

Reference answer:

The main similarities: Lock to complete all the functions synchronized achieved
major differences: Lock more accurate than synchronized thread semantics and better performance. synchronized automatically release the lock, and Lock requires the programmer must manually released and must be released in the finally clause.

2. JAVA how to ensure that the N threads can access N resources, but do not lead to deadlock? 

Inspection point: Deadlock

Reference answer:

When using multiple threads, a very simple way to avoid deadlock is: Specifies the order of acquisition of the lock, and forcibly acquiring a lock thread in the specified order. Thus, if all threads are locking and releasing locks in the same order, it will not deadlock appeared.

Deadlock prevention, a deadlock condition occurs four previously destroyed. Mutex impossible destroyed, the following three methods:

1. Request damage and keep the conditions, processes must apply to all resources To request resources are idle, this approach will make serious waste of resources (some resources may be used only when the end or the beginning of the run, do not even use) allow the process to obtain the necessary initial resources, they start to run, during operation and then gradually release the resources they possess, such a process has the task of copying data to disk before printing, pre-only disk resources without the need to obtain get printer resources, to be completed before copying freed disk resources. This method is better than the first method, resource utilization will rise.

2. The damage can not be preempted conditions, this method is a large cost to implement complex.

3. wait destruction through the bad conditions of the order requested resource for the processes to make a provision to avoid the wait for each other. This method is the utilization of resources is higher than the first two, but there will be a problem to join the pre-specified for the device serial number, new equipment, followed by user programming is limited.

3. What is meant by a deadlock (deadlock)?

Inspection point: Thread Deadlock

Reference answer:

Thread two or more threads are waiting for each other when finished to continue down the deadlock occurs. The result is that these threads are caught in endless waiting.

For example, if a thread locked A, B and attempts to perform the lock, while the thread has locked a B 2, A and then attempts to perform the lock, then a deadlock occurs. Thread 1 never get B, thread 2 also never be A, and they never know such a thing happened. In order to obtain objects with each other (A and B), they will always go obstruction. This situation is a deadlock.

4. Please explain the difference between locking and synchronization.

Inspection point: Lock

Reference answer:

Usage differences:
the synchronized method may be applied to, you may also be loaded on a particular block of code, and lock needs to specify the display start position and end position.
synchronized to the JVM execution is managed, the locking by the code lock is achieved, it is more accurate than the semantics of synchronized threads.
Difference in performance:
the implementation class ReentrantLock lock interface, and not only has the same synchronized and memory concurrency semantics, but also more time-out to acquire the lock, the time lock, waiting and interrupt lock.
In the competition is not very intense situation, the performance is better than ReentrantLock synchronized, synchronized performance of the competitive situation will fall very fast, and ReentrantLock is basically unchanged.
Different locking mechanisms:
the synchronized acquire and release locks are in the way of a block structure, when acquiring multiple locks must be released in reverse order, and are automatically unlocked. Lock and then require developers to manually release and must be released finally in, otherwise it will lead to deadlock.

5. Please explain synchronized reentrant how to achieve.

Inspection point: Lock

Reference answer:

Each lock is associated with a thread holder and a counter. Counter to 0 indicates that the lock is not held by any thread, any thread may acquire the lock and call the appropriate method when. When a thread request succeeds, JVM will write down the thread holding the lock, and the counter is 1. At this time, other threads requesting the lock, it must wait. And the thread holding the lock request again if the lock, you can get the lock again and the counter is incremented. When a thread exits a synchronized method / block, the counter is decremented, the counter is 0 if the lock is released.

6. Please talk about fair and unfair lock lock in the implementation process in reetrantlock is like.

Inspection point: Lock

Reference answer:

If a lock is fair, then lock acquisition order should be in line with the absolute chronological order requests, FIFO. For non-fair locks, as long as the CAS set up synchronization status it is successful, the current thread acquired the lock, but the lock also need a fair judge whether the current node has a predecessor node, if there is, then there is a thread request earlier than the current thread to acquire the lock, so after the precursor thread to wait to obtain and release the lock to continue to acquire locks.

Guess you like

Origin blog.csdn.net/qq_31194443/article/details/93645088