[Java] Singleton Pattern using synchronized

2023. 7. 4. 09:42Tip

728x90

텍스트

더보기
import ...

// Singleton
public class EntryServer implements Server {
    private static EntryServer INSTANCE = null;

    private EntryServer(int port) {...}

    public static EntryServer create(int port) {
        synchronized (EntryServer.class) {
            if (INSTANCE == null)
                INSTANCE = new EntryServer(port);
        }
        return INSTANCE;
    }

}​

 

728x90