Name for this pattern? (Answer: lazy initialization with double-checked locking)

前端 未结 4 1078
谎友^
谎友^ 2021-01-23 02:45

Consider the following code:

public class Foo
{
    private static object _lock = new object();

    public void NameDoesNotMatter()
    {
        if( SomeDataDo         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-23 03:16

    The part that I'm especially interested in identifying as a pattern is the check -> lock -> check.

    That is called double-checked locking.

    Beware that in older Java versions (before Java 5) it is not safe because of how Java's memory model was defined. In Java 5 and newer changes were made to the specification of Java's memory model so that it is now safe.

提交回复
热议问题