Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Lazy initialization
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Java=== {{Warning |This example is not thread-safe, see the [[Talk:Lazy initialization#Java code: thread safety flaw and pertinence|talk page]]. Instead see the examples in [[Double-checked locking#Usage in Java]].}} This example is in [[Java (programming language)|Java]]. <syntaxhighlight lang="java"> import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; public class Program { /** * @param args */ public static void main(String[] args) { Fruit.getFruitByTypeName(FruitType.banana); Fruit.showAll(); Fruit.getFruitByTypeName(FruitType.apple); Fruit.showAll(); Fruit.getFruitByTypeName(FruitType.banana); Fruit.showAll(); } } enum FruitType { none, apple, banana, } class Fruit { private static Map<FruitType, Fruit> types = new HashMap<>(); /** * Using a private constructor to force the use of the factory method. * @param type */ private Fruit(FruitType type) { } /** * Lazy Factory method, gets the Fruit instance associated with a certain * type. Instantiates new ones as needed. * @param type Any allowed fruit type, e.g. APPLE * @return The Fruit instance associated with that type. */ public static Fruit getFruitByTypeName(FruitType type) { Fruit fruit; // This has concurrency issues. Here the read to types is not synchronized, // so types.put and types.containsKey might be called at the same time. // Don't be surprised if the data is corrupted. if (!types.containsKey(type)) { // Lazy initialisation fruit = new Fruit(type); types.put(type, fruit); } else { // OK, it's available currently fruit = types.get(type); } return fruit; } /** * Lazy Factory method, gets the Fruit instance associated with a certain * type. Instantiates new ones as needed. Uses double-checked locking * pattern for using in highly concurrent environments. * @param type Any allowed fruit type, e.g. APPLE * @return The Fruit instance associated with that type. */ public static Fruit getFruitByTypeNameHighConcurrentVersion(FruitType type) { if (!types.containsKey(type)) { synchronized (types) { // Check again, after having acquired the lock to make sure // the instance was not created meanwhile by another thread if (!types.containsKey(type)) { // Lazy initialisation types.put(type, new Fruit(type)); } } } return types.get(type); } /** * Displays all entered fruits. */ public static void showAll() { if (types.size() > 0) { System.out.println("Number of instances made = " + types.size()); for (Entry<FruitType, Fruit> entry : types.entrySet()) { String fruit = entry.getKey().toString(); fruit = Character.toUpperCase(fruit.charAt(0)) + fruit.substring(1); System.out.println(fruit); } System.out.println(); } } } </syntaxhighlight> '''Output''' <pre> Number of instances made = 1 Banana Number of instances made = 2 Banana Apple Number of instances made = 2 Banana Apple </pre>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)