Coder Social home page Coder Social logo

mafinlib's People

Contributors

alessandrognoatto avatar

Watchers

 avatar  avatar

mafinlib's Issues

Maybe ConcurrentHashMap or Static Initialization is an alternative.

I am not sure if this class is truly useful - see my PM - but as a general comment you could use a ConcurrentHashMap here:

private Map<Class,Object> mapHolder = new HashMap<Class,Object>();

Instead of using a Singleton that maintains a Map to other Singletons you may consider a static initialiser:
https://stackoverflow.com/questions/3635396/pattern-for-lazy-thread-safe-singleton-instantiation-in-java

Check double checked locking / Consider lazy initialiser or static initializer

At

if(instance == null){

the double checked locking idiom is used. This is broken in this form (since the member is non volatility). To fix it the member (singleton) should be volatility (also note that your singleton is not immutable)!

Background Information on Double Checked Locking

Double Checked Locking was broken in Java 1.4 and fixed in 1.5 (around 2005) using the keyword volatility.
See
https://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
and
http://www.angelikalanger.com/Articles/EffectiveJava/41.JMM-DoubleCheck/41.JMM-DoubleCheck.html
Note that Double Checked locking is also broken in C++ and this is fixed in C++ 11 (around 2011), see
http://preshing.com/20130930/double-checked-locking-is-fixed-in-cpp11/

Improvements

For lazy init I would recommend to use a standard LazyInitializer:
https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/concurrent/LazyInitializer.html
implementing a ConcurrentInitializer:
http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/concurrent/ConcurrentInitializer.html

But for Singlestons lazy initialisation is trivial because you can use a static initializer - note that classed are not loaded until they are needed (Java does Lazy w.r.t. class loading) and a static initalizer is executed upon class loading - and thread safe by spec:
https://stackoverflow.com/questions/3635396/pattern-for-lazy-thread-safe-singleton-instantiation-in-java

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.