Interview - Lock

First, please describe briefly the similarities and differences between synchronized with java.util.concurrent.locks.Lock of?

A: The same point: Lock to complete all the functions synchronized achieved;

Different points: Lock more accurate than synchronized thread semantics and better performance. synchronized automatically releases the locks, and Lock requires the programmer must manually released and must be released in the finally clause.

Two, Java How to ensure n n threads can access a resource, but do not lead to deadlock?

A: When using multiple threads, a very simple way to avoid the deadlock: the specified order to acquire 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:

① damage when requested and maintain the conditions, the process must be requested and all other resources are idle resources to apply this approach will make serious waste of resources. Allows a process to obtain the required resources early, 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 printer resources, to be freed have been copied in the disk resources.

② damage can not seize conditions, this method is a large cost to implement complex.

③ destruction of circular wait condition, the order of the requested resource for the processes to make a provision to avoid the wait for each other.

Third, ask what is a deadlock?

A: Two or more threads are waiting for the other thread is finished to proceed down the time it happened deadlock. 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 get each other's objects (A and B), they will never clog it, this situation is a deadlock.

Fourth, please explain the difference between locking and synchronization?

A: The difference in usage:

That can be applied to the synchronized method, the specific code may also be loaded. The lock needs to specify the display start position and end position.

It is hosting synchronized to the JVM execution. Locking lock is achieved through the code, it is more accurate than the semantics of synchronized threads.

Difference in performance:

ReentrantLock lock interface implementation class, not only has the same synchronized concurrency and memory semantics, but also more time-out to acquire the lock, the time lock, waiting and interrupt lock.

Different locking mechanisms: 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. And require developers to manually lock is released and must be released finally in, otherwise it will lead to deadlock.

 

Published 16 original articles · won praise 26 · views 7391

Guess you like

Origin blog.csdn.net/qq_41629684/article/details/104072314