还在自己写Java懒加载代码吗?

阅读更多

Java懒加载工具类

 

com.google.common.base.Suppliers.memoize(Supplier)

com.google.common.base.Suppliers.memoizeWithExpiration(Supplier, long, TimeUnit)

 

 

package tool;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;

public class RawTest {

	public static void main(String[] args) throws Exception {
		Supplier supplier = Suppliers.memoize(() -> supply());
		System.out.println(supplier.get());
		System.out.println(supplier.get());
		System.out.println(supplier.get());
	}

	public static String supply() {
		System.out.println("called supply");
		return "Hello World";
	}

}

 

结果

 

写道
called supply
Hello World
Hello World
Hello World

 

你可能感兴趣的:(还在自己写Java懒加载代码吗?)