Answers for "Java Multithread Lazy Initialization"

0

Java Multithread Lazy Initialization

class ResourceFactory {
  private volatile Resource resource;

  public Resource getResource() {
    Resource localResource = resource;
    if (localResource == null) {
      synchronized (this) {
        localResource = resource;
        if (localResource == null) {
          resource = localResource = new Resource();
        }
      }
    }
    return localResource;
  }

  static class Resource {
  }
}
Posted by: Guest on October-24-2021

Code answers related to "Java Multithread Lazy Initialization"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language