Design patterns
धीरे लोड हो रहा है
खोज…
परिचय
उत्सुक लोडिंग महंगी है या लोड की जाने वाली वस्तु की आवश्यकता नहीं हो सकती है
जावा लोडिंग लोड हो रहा है
मुख्य से बुलाओ ()
// Simple lazy loader - not thread safe
HolderNaive holderNaive = new HolderNaive();
Heavy heavy = holderNaive.getHeavy();
Heavy.class
/**
*
* Heavy objects are expensive to create.
*
*/
public class Heavy {
private static final Logger LOGGER = LoggerFactory.getLogger(Heavy.class);
/**
* Constructor
*/
public Heavy() {
LOGGER.info("Creating Heavy ...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
LOGGER.error("Exception caught.", e);
}
LOGGER.info("... Heavy created");
}
}
HolderNaive.class
/**
*
* Simple implementation of the lazy loading idiom. However, this is not thread safe.
*
*/
public class HolderNaive {
private static final Logger LOGGER = LoggerFactory.getLogger(HolderNaive.class);
private Heavy heavy;
/**
* Constructor
*/
public HolderNaive() {
LOGGER.info("HolderNaive created");
}
/**
* Get heavy object
*/
public Heavy getHeavy() {
if (heavy == null) {
heavy = new Heavy();
}
return heavy;
}
}
Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow