Some of the differences and selection considerations (40) lock and the synchronized

Lock and synchronized selection

  1 ) Lock is an interface, and synchronized are Java keywords, synchronized built-in language;

  2 ) the synchronized when an exception occurs, it will automatically release the thread owns the lock, it will not lead to a deadlock phenomenon; and Lock when an exception occurs, if there is no initiative by unLock () to release the lock, then it is likely to cause deadlock Therefore using lock required when finally releases the lock block;

  3 ) Lock lets wait for the lock thread interrupt response, and synchronized but not using synchronized , the waiting threads will wait forever, can not respond to interrupts;

  4 ) through Lock you can know without success to obtain a lock, and synchronized it can not be done.

  5 ) Lock can improve the efficiency of multiple threads read operation.

  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), then Lock the performance is much better than the synchronized . So, to select appropriate in accordance with the specific use.

Guess you like

Origin www.cnblogs.com/paradis/p/11432016.html