範例:

public class AutoStudent {

    // Singleton
    private static AutoStudent instance = null ;

    public static AutoStudent getInstance() {
        if ( instance == null ) {
            synchronized ( AutoStudent.class ) {
                if ( instance == null ) {
                    instance = new AutoStudent();
                }
            } // synchronized
        }
        return instance;
    } // getInstance()

    private AutoStudent()
    {
    }

} // public class AutoStudent

 

參考:[良葛格] Singleton 模式
更多:[JWorld@TW] [DP]Singleton pattern, Double-checked Locking pattern (by Biologic) [精華]
         [University of Maryland]-[Computer Science]-[Bill Pugh] The "Double-Checked Locking is Broken" Declaration

 

arrow
arrow

    Robert 發表在 痞客邦 留言(0) 人氣()