1. As is the case for most optimizations, the best advice for lazy initialization is “don’t do it unless you need to”.
2. If a field is accessed only on a fraction of the instances of a class and it is costly to initialize the field, then lazy initialization may be worthwhile. The only way to know for sure is to measure the performance of the class with and without lazy initialization.
3. If you use lazy initialization to break an initialization circularity, use a synchronized accessor.
4. If you need to use lazy initialization for performance on a static field, use the lazy initialization holder class idiom. This idiom (also known as the initialize-on-demand holder class idiom) exploits the guarantee that a class will not be initialized until it is used:
// Lazy initialization holder class idiom for static fields private static class FieldHolder { static final FieldType field = computeFieldValue(); } static FieldType getField() { return FieldHolder.field; }
When the getField method is invoked for the first time, it reads FieldHolder.field for the first time, causing the FieldHolder class to get initialized. A modern VM will synchronize field access only to initialize the class. Once the class is initialized, the VM will patch the code so that subsequent access to the field does not involve any testing or synchronization.
5. If you need to use lazy initialization for performance on an instance field, use the double-check idiom. This idiom avoids the cost of locking when accessing the field after it has been initialized. The idea behind the idiom is to check the value of the field twice (hence the name double-check): once without locking, and then, if the field appears to be uninitialized, a second time with locking. Only if the second check indicates that the field is uninitialized does the call initialize the field. Because there is no locking if the field is already initialized, it is critical that the field be declared volatile:
// Double-check idiom for lazy initialization of instance fields private volatile FieldType field; FieldType getField() { FieldType result = field; if (result == null) { // First check (no locking) synchronized(this) { result = field; if (result == null) // Second check (with locking) field = result = computeFieldValue(); } } return result; }
What the local variable does is to ensure that field is read only once in the common case where it’s already initialized. While not strictly necessary, this may improve performance and is more elegant by the standards applied to low-level concurrent programming.
6. Occasionally, you may need to lazily initialize an instance field that can tolerate repeated initialization. If you find yourself in this situation, you can use a variant of the double-check idiom that dispenses with the second check. It is, not surprisingly, known as the singlecheck idiom. Note that field is still declared volatile. If you don’t care whether every thread recalculates the value of a field, and the type of the field is a primitive other than long or double, then you may choose to remove the volatile modifier from the field declaration in the single-check idiom. This variant is known as the racy single-check idiom. It speeds up field access on some architectures, at the expense of additional initializations (up to one per thread that accesses the field). This is definitely an exotic technique, not for everyday use. It is, however, used by String instances to cache their hash codes.
相关推荐
Item 83: Use lazy initialization judiciously Item 84: Don’t depend on the thread scheduler 12 Serialization Item 85: Prefer alternatives to Java serialization Item 86: Implement Serializable with ...
懒:Kule Lazy4 CSS框架
A History of Haskell - Being Lazy With Class
lazyload1.9.3,图片延迟随滚动条显示。$(function() { $("img.lazyload").lazyload()});
Generator Lazy Demo 跟着例子在3分钟内玩转Generator Lazy。 Useage 向导模式创建 在任意目录中执行yo lazy,然后一路NEXT即可。 在包含配置文件的项目中使用 命令: yo lazy cd build-with-config-exist yo lazy 在...
React Lazy Loading - 很容易与 React 集成到 Lazyload 组件、图像等。它会监视元素并告诉您元素何时进入视口。 这样可以在视口中的组件和初始加载减少时执行任何操作。 实现“无限滚动”网站,在您滚动时加载和...
Lazy是一个帮助查看Salesforce部署期间发生的顶点运行测试的细节的工具。 这不是Salesforce.com产品。 在任何情况下,开发者均不对因使用本软件及其文档而引起的任何直接,间接,特殊,意外或间接损失(包括利润损失...
Exopite-Lazy-Load-XT-WordPress-Plugin Exopite Lazy Load XT使用Ressio Lazy Load XT jQuery插件延迟加载图像,视频和iframe。 作者:乔·萨莱(Joe Szalai) 版本:20190521 插件URL: : 作者网址: : 许可...
后台机器人 向后移植将请求拉入发布分支的bot 配置以下环境变量以进行部署: BACKPORT_GH_API = 'api_token_for_your_bot' BACKPORT_MAINTAINER = 'bot_listens_to_this_user' BACKPORT_BOT_GH_NAME = 'bot_github...
LazyBone Micro框架 这是来自ribs.js示例的todoapp的演示。 它使用LazyRecord作为ORM,并通过Roller Router处理RESTful请求。 引导程序 安装洋葱: $ curl http://install.onionphp.org/ | bash $ onion bundle ...
RDF :: Lazy-延迟键入对RDF数据的访问 概要 ### How to create a graph $g = RDF::Lazy->new( rdf => $data, # RDF::Trine::Model or ::Store (by reference) namespaces => { # namespace prefix, RDF::NS or RDF...
主题个人专业版主题Lazy5basic(又名Personal Pro)-Concrete5 CMS软件包文件: :
懒惰的一个宏,用于在Rust中声明惰性评估的静态值。 使用此宏,可能具有static ,这些static要求在运行时执行代码才能进行初始化。 这包括需要堆分配的任何...例子use lazy_static :: lazy_static;use std :: collec
注:LazyImg 必须定义lazy-data属性,属性值是src的图片路径 引入JS文件:<script type="text/javascript" src="js/lazyImg.v1.0.js"> 默认情况下: 在IMG中满足以任何一个条件,都会加载图片; 1、没有class属性 2...
react-use-lazy-load-image使用为延迟加载的图像提供高性能解决方案,该图像不涉及滚动事件侦听器。 IntersectionObserver API仍然很新,因此较旧的浏览器可能不支持此功能,但是对于这些用例,可以使用一些不错的...
var Lazy = require ( 'lazy' ) ; var lazy = new Lazy ; lazy . filter ( function ( item ) { return item % 2 == 0 } ) . take ( 5 ) . map ( function ( item ) { return item * 2 ; } ) . join ( ...
LazyTableImages.zip V 1.0, 2009-11-16 这是苹果apple的iphone官方例子,帮助你学习UITableView和显示RSS文本,异步方式加载图片...... 这个例子 分阶段地载入和显示一个UITableView. 一开始载入相关的RSS文本,...
LazyK:更新了LazyK源分发
#Lazy在网络上加载图片 该存储库包含一些使用多种方法为网络延迟加载图像的简单示例:普通JavaScript,jQuery,[be] Lazy.js,LazyLoad.js,Lazy Load XT和Unveil。
懒惰的图像用于延迟加载图像的轻量级 javascript 插件用安装模块: npm install lazy-images需要模块: var lazy = require('lazy-images')([IMAGE_SELECTOR]);IMAGE_SELECTOR 默认为“.lazy”笔记为了让这个模块...