© Ralph Lecessi 2019
Ralph LecessiFunctional Interfaces in Javahttps://doi.org/10.1007/978-1-4842-4278-0_14

14. Use in Atomic Calculations

Ralph Lecessi1 
(1)
Kendall Park, NJ, USA
 
It is unsafe for execution threads to share data without protecting them with locks or some other form of synchronization. In the following program, two parallel threads share an unprotected int variable called num. During an iteration of thread cf2's for loop, the value -1490 was read from num and stored in local variable x. The JVM and the operating system then allow thread cf1 to run for a while, changing the value of num to 8290. When thread cf2 regains control and reads back num, it has the value 8290 instead of -1490 since the value of num has been corrupted by thread ...

Get Functional Interfaces in Java: Fundamentals and Examples now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.