Multi-threaded interview high frequency test, we published different opinions, strengthen the impression,
CodePudding user response:
Is a reentrant lock, synchronized use after the lock is automatically released, already need to manually release, already can set the timeout or something more flexible point, synchronized relatively simple easy to use, other aspects should be about the sameCodePudding user response:
Synchronized is Java's built-in keywords, use rise very convenient, already is to provide a class, in the JDK Java began to support the synchronized its efficiency compared with the already low, behind the optimized gradually, and already quite efficiency, the realization of the already can refer to the JDK source code, inherited AbstractQueueSychronizer, and use the unsafe park and unpark sleep and wake up the thread of a class, through compareAndSwap methods achieve concurrency mechanisms, namely acquiring a lock is to actually change a variable's value in the memory, change successful representatives for the lock, if the variable has been modified by another thread, the current thread can only wait for other threads to modify, the current thread loop to judge or dormant, Java objects in MarkWord, synchronized implementation and its related, synchronized with the enter queues and wait queue, acquiring a lock into the enter queue to compete, have been acquiring a lock by wait method after releasing the lock will enter the wait queue, through the notify method after awakening will re-enter the enter lock queue competition,Ps: about how to ensure the safety of concurrent the underlying implementation in the Linux 0.11 are implemented by disabling interrupts, very simple and crude, there are instructions to ensure that the atom, by locking memory, software on the things that are difficult to implement in hardware is easy to implement, for reference by the above content, some details can't remember, need to find research!!!!!!