Using synchronized to Avoid Race Conditions

Learn how synchronized can be used to avoid race conditions.

We'll cover the following

Use of synchronized

The incorrect program behavior in the previous lesson is due to more than one thread accessing the same mutable data (and at least one of them modifying it). One way of avoiding these race conditions is to mark the common code with the synchronized keyword. The program will work correctly with the following change:

Get hands-on with 1200+ tech skills courses.