/*
 * oxUtil is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */
package org.xdi.util.init;


/**
 * Allow class to avoid parallel initializations
 * 
 * @author Yuriy Movchan 11/14/2014
 */
public abstract class Initializable {

	private static Object lock = new Object();
    
    private boolean initialized = false;

    public void init() {
    	if (!this.initialized) {
			synchronized (lock) {
                if (!this.initialized) {
                    initInternal();
                    this.initialized = true;
                }
			}
    	}
    }
    
    public synchronized void reinit() {
    	initInternal();
        this.initialized = true;
    }
    
    protected abstract void initInternal();
}

@@ -166,16 +166,65 @@
 .init;%0A%0A
+import java.util.concurrent.locks.ReentrantLock;%0A
 %0A/**%0A * 
@@ -365,52 +365,58 @@
 ate 
-static Object lock = new Object();%0A    %0A    
+final ReentrantLock lock = new ReentrantLock();%0A%0A%09
 priv
@@ -449,20 +449,17 @@
 false;%0A%0A
-    
+%09
 public v
@@ -471,20 +471,17 @@
 nit() %7B%0A
-    
+%09
 %09if (!th
@@ -505,197 +505,164 @@
 %0A%09%09%09
-synchronized (lock) %7B%0A                if (!this.initialized) %7B%0A                    initInternal();%0A                    this.initialized = true;%0A                %7D%0A%09%09%09%7D%0A    %09%7D%0A    %7D%0A    %0A    
+lock.lock();%0A%09%09%09try %7B%0A%09%09%09%09if (!this.initialized) %7B%0A%09%09%09%09%09initInternal();%0A%09%09%09%09%09this.initialized = true;%0A%09%09%09%09%7D%0A%09%09%09%7D finally %7B%0A%09%09%09%09lock.unlock();%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A%09
 publ
@@ -693,20 +693,17 @@
 nit() %7B%0A
-    
+%09
 %09initInt
@@ -715,16 +715,10 @@
 ();%0A
-        
+%09%09
 this
@@ -742,23 +742,13 @@
 ue;%0A
-    %7D%0A    %0A    
+%09%7D%0A%0A%09
 prot

Use reentrant lock