eugenp / tutorials

Just Announced - "Learn Spring Security OAuth":

Home Page:http://bit.ly/github-lsso

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[QUERY] Volatile Example

DivyaSaravanan opened this issue · comments

Article and Module Links
Guide to the Volatile Keyword in Java.

Describe the Issue

The volatile example provided works fine, even without adding volatile keyword.

https://github.com/eugenp/tutorials/blob/master/core-java-modules/core-java-concurrency-simple/src/main/java/com/baeldung/concurrent/volatilekeyword/TaskRunner.java#L6

To Reproduce
Steps to reproduce the behavior:

  • Go to Task Runner

  • Remove the Volatile keyword next to the field ready.

  • Execute the code, still, the code works fine.

Expected Behavior
If the volatile keyword is not present, ideally the reader thread and main thread would have local copies of the ready variable, and the while loop would not exit.

Does the above vary in cases, and not always expected?

Screenshots
If applicable, add screenshots to help explain your problem.

Environment (please complete the following information):

  • OS: [e.g. Windows]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Additional Context
Add any other context about the issue here.