What is the volatile keyword useful for?

前端 未结 23 3240
闹比i
闹比i 2020-11-21 05:32

At work today, I came across the volatile keyword in Java. Not being very familiar with it, I found this explanation.

Given the detail in which that arti

23条回答
  •  半阙折子戏
    2020-11-21 06:05

    “… the volatile modifier guarantees that any thread that reads a field will see the most recently written value.” - Josh Bloch

    If you are thinking about using volatile, read up on the package java.util.concurrent which deals with atomic behaviour.

    The Wikipedia post on a Singleton Pattern shows volatile in use.

提交回复
热议问题