From the course: Advanced Java: Threads and Concurrency

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Synchronization: Purpose and use

Synchronization: Purpose and use - Java Tutorial

From the course: Advanced Java: Threads and Concurrency

Synchronization: Purpose and use

- [Narrator] When thread share data issues like data races and race conditions could possibly occur. Though data racers can sometimes cause race conditions. The two are not dependent on each other. For instance, your code could be a possible candidate for a data race to occur, but it might not necessarily lead to a race condition. Or it could perhaps be the other way around. Using the volatile keyword on variables that are shared by multiple threads could help prevent or reduce data races to some extent. It guarantees visibility by always reading from and writing to variable values from and to the main memory and not the CPU registers. This prevents threads from accessing shared data. However, this visibility guarantee is not sufficient when threads that share a piece of data are not just reading it, but also writing it. This is because volatile does not lock the shared piece of data or variable from being read or written to by one thread, from being read or written to by other…

Contents