8

I was just trying to understand optimistic and pessimistic locking mechanisms and came across the definition of them at https://en.wikipedia.org/wiki/Lock_(database)

If I try to relate the concepts to java instead of a database, am I right in saying that a synchronized usage is always pessimistic and a CAS (AtomicInteger and other classes) usage is always optimistic?

1 Answer 1

8

Am I right in saying that a synchronized usage is always pessimistic and a CAS (AtomicInteger and other classes) usage is always optimistic ?

Yes you are correct.

Traditional locking mechanisms e.g. using synchronized keyword in java, is said to be pessimistic technique of locking or multi-threading.

The optimistic approach is like the old saying, “It is easier to obtain forgiveness than permission”, where “easier” here means “more efficient”. CAS is an example of optimistic technique. StampedLock also has support for optimistic locking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.