Previous | Next --- Slide 29 of 65
Back to Lecture Thumbnails
SR_94

So the difference b/w synchronized and atomic keywords is that the former refers to a blocking scheme and the latter do an unblocking? Or a better way to say that the former is imperative and the latter is declarative?

kayvonf

@SR_94. Synchronized is a Java keyword that essentially asserts mutual exclusion. Specifically, from the documentation:

https://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html

  • It is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.
  • When a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads.

Atomic is this lecture is used to declare transactional semantics. It is possible to concurrently execute two transactions on a data structure provided those transactions do not conflict. In contrast synchronized prevents non-atomic execution of the function via ensuring mutual exclusion on the object.