O'Reilly logo

Java 9 Concurrency Cookbook - Second Edition by Javier Fernández González

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

The following screenshot shows the output of the example:

The application doesn't finish its execution because the task thread has not finished. When we change the value of volatileFlag-as its flag attribute is marked as volatile--the new value is written in the main memory and VolatileTask accesses the value immediately and finishes its execution. On the contrary, when you change the value of the flag object--as its flag attribute is not marked as volatile-the new value is stored in the cache of the main thread and the task object doesn't see the new value and never ends its execution. The volatile keyword is important not ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required