Tuesday, November 17, 2015

LinkedIn: Implement a thread-safe Singleton

Question:
Implement a thread-safe Singleton.

Code (Java):
import java.io.*;
import java.util.*;

public class Singleton {
    private static Singleton singleton = null;
    
    private Singleton() {
        
    }
    
    public synchronized static Singleton getInstance() {
        if (singleton == null) {
            singleton = new Singlton();
        }
        
        return singleton;
    }
}


No comments:

Post a Comment