The difference Synchronized at the Lock

The difference Synchronized at the Lock

  1. Usage is not the same. either added to the synchronized method may also be loaded on a particular block of code, a subject in need parentheses indicates lock. Lock need to display the designated start position and end position. synchronzied is hosting jvm to be executed, Lock locking is achieved through code.

  2. In terms of performance, if not fierce competition for resources, the performance of both is similar, but when the competition is very fierce resource (ie a large number of threads simultaneously competition), this time the performance is much better than Lock synchronized. So, to select appropriate in accordance with the specific use.

  3. The lock mechanism is not the same. Synchronized manner to obtain and release the lock structure is in a block, and the lock is automatically released. The Lock the need to develop staff to manually release, and must be released in a finally block, otherwise it would cause deadlock problems.

  4. Lock is an interface, not the Java language built; synchronized keyword Java language and is therefore built-in features.

  5. When using synchronized user is not required to manually release the lock, after the synchronized method or synchronized when executing the code block or exception occurs, the system will automatically make the holding thread releases the lock; Lock and when an abnormality occurs, if not actively pass unLock () to release the lock, it is likely to result in deadlock, it is necessary to release the lock in a finally block the use of lock;

  6. Lock lets wait for the lock thread in response to interrupts, but it's not synchronized, when using synchronized, waiting threads will wait forever, can not respond to interrupts; by Lock can know without success to obtain a lock, and synchronized it can not be done. Lock multiple threads can improve the efficiency of the read operation.

Guess you like

Origin www.cnblogs.com/simon-1024/p/11751500.html