status flag is set in afterInit() by the background // Initializer thread and must be volatile to ensure proper // cross thread state change notification private volatile Status status = Status.INIT; public Service() { // Allow the service/object to construct completely to avoid // potential memory, or incomplete object initializations // then call serviceInitialize() on this object } public void serviceInitialize() { new Initializer(this).initialize(); } public void tryInit() throws Exception { // attempt an initialization here ... } public void afterInit() { status = Status.UP; } public Status getStatus() { return status; } public void aUsefulMethod(String arg1, int arg2) { // Always make sure the service is ready for use. if (status != Status.UP) { throw new IllegalStateException("Not yet initialized"); } ... // Do something interesting now. } }
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.