class Main { private final Map map = new ConcurrentHashMap<>(); public Object getOnce(int idx) { Object obj = map.get(idx); if (obj == null) { synchronized(this) { obj = map.get(idx); if (obj == null) { obj = new Object(); map.put(idx, obj); } } } return obj; } }