SocketAcceptor initialization is using Boolean object as both lock and flag. This approach has 2 major problems:
1) Public object such as Boolean.FALSE should not be used for synchronization. If it is used as lock in multiple places it can lead to performance degradation or deadlock.
2) Synchronization is done on Boolean.FALSE before initialization and on Boolean.TRUE after initialization but synchronization on 2 different locks do not constitute "happens-before" relationship. It means thread coming shortly after initialization can see changed flag and use Boolean.TRUE for locking and so it uses different lock than the lock used during initialization. As a result this thread can see objects created and updated during initialization in inconsistent state.
The problem can be fixed by using private not-share object as a lock and boolean value as flag.
|