Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Effective Java - lazy initialization     - quotacheck command file can not be created aquota.user and aquota.group solutions (Linux)

- OpenCV cvFindCornerSubPix () to find sub-pixel Corner (Programming)

- PL / SQL in forall simple test (Database)

- Why learn and use C language (Programming)

- How to install Laravel PHP framework on CentOS 7 / Ubuntu 15.04 (Server)

- Use ldap implement Windows Remote Desktop Ubuntu Linux (Linux)

- MySQL remote connection settings (Database)

- Linux install the Java Runtime Environment and the output Hello World under (CentOS) (Linux)

- To generate a certificate using OpenSSL under Linux (Server)

- Debian 8.1 (amd64) deployed Memcached (Server)

- php performance monitoring module XHProf (Linux)

- Nginx log cutting and MySQL script regular backup script (Server)

- Set up MySQL master and slave servers under Ubuntu 14.04 (Server)

- Ubuntu 12.04 kernel configuration automatically restart and crash dump (Linux)

- Elaborate .NET Multithreading: Concepts (Programming)

- Linux set to select the appropriate level of security of the network according to deployment (Linux)

- SecureCRT use the configuration detailed tutorial (Linux)

- CentOS 5.10 installed Oracle 11G R2 (Database)

- Java 8 stream parsed into SQL (Programming)

- Install minimize RHEL / CentOS 7 some things need to do (Linux)

  Effective Java - lazy initialization
  Add Date : 2018-11-21      
  Lazy initialization (lazy initialization), that is, when you actually began to be used to initialize skills.
 Whether static or instance, you can be lazy initialization.
 Its essence is to weigh the cost of access and initialization overhead between.
 After all, is an optimization technique, improper use will play the opposite effect.
 Especially in multithreaded scenarios such anti-effect will be particularly evident, because we want to delay the initialization of field synchronization.

A first step begins, if the initialization overhead is not worth mentioning, we only need to ensure that their immutable:
private final FieldType field1 = computeFieldValue ();


If there is consultation, initialization overhead may make people care about, here is the easiest way to directly access method declaration Riga synchoronized modified in this way will maximize the access cost:
private FieldType field2;

synchronized FieldType getField2 () {
    if (field2 == null)
        field2 = computeFieldValue ();
    return field2;

    private static FieldType computeFieldValue () {
    return new FieldType ();


If you want to change nothing but static plus static modification, but for static initialization, we can use the class holder ways:
private static class FieldHolder {
    static final FieldType field = computeFieldValue ();

static FieldType getField3 () {
    return FieldHolder.field;

private static FieldType computeFieldValue () {
    return new FieldType ();


It makes them feel good, we do not have additional sync only when accessing getField3 of FieldHolder will be initialized.
 Therefore, this case belongs to no increase in the cost of access also ensures delay characteristics.

Try to optimize the cost of access at the instance of the field, the most classic is the double-check, and this thing often appear in the pen questions:
private volatile FieldType field4;

FieldType getField4 () {
    FieldType result = field4;
    if (result == null) {
        synchronized (this) {
            result = field4;
            if (result == null)
                field4 = result = computeFieldValue ();
    return result;
private static FieldType computeFieldValue () {
    return new FieldType ();


Using a result code local variables, although doing so is not necessary, but it ensures that under field conditions have been read once initialized, the efficiency can be improved a little.

These are some of the common ways of lazy initialization.
 Lazy initialization looks good, but it is recommended to weigh the cost of accessing and creating, for instance field using double-check, static field use holder class, to ensure that the check-then-action atomicity when multiple threads access.
- vector C ++ sequence containers (Programming)
- Linux System Getting Started Learning: The Linux logrotate (Linux)
- JDK installation under CentOS (Linux)
- OpenGL Superb Learning Notes - GLSL language foundation (Programming)
- GitLab Installation Guide -Ubuntu 14.04 LTS (Server)
- Oracle database NUMBER (x, y) data types (Database)
- Ubuntu 14.04 + xRDP + Xfce implement Windows Remote Desktop Connection (Linux)
- Scope of variables in C # (Programming)
- JavaScript common array manipulation functions and usage (Programming)
- Echo Command Examples (Linux)
- Linux host dual LAN transceiver package ARP problem (Linux)
- Ubuntu 14.04 CodeBlocks configuration manually compile Boost 1.57.0 (Linux)
- KVM QEMU virtual machine installation configuration under CentOS (Linux)
- Zabbix monitors the status of TCP connections (Server)
- Moosefs Distributed File System Configuration (Server)
- Transfer files and permissions from Windows to Linux system by Samba (Linux)
- Open container cluster management system architecture and components introduced Kubernetes (Server)
- Wireshark basic introduction and learning TCP three-way handshake (Linux)
- On Android running ClojureScript (Linux)
- Oracle 11g partition maintenance (Nice) - Truncating And Partitions (Database)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.