Synchronization Object

A synchronization object allows threads and processes running in parallel (and possibly on different PhysicalMachine s or VirtualMachine s) to coordinate their activities. This allows data to be manipulated without it becoming garbled or inconsistent.

Important in AutomatedIntelligence in indicating the completion level of the operation and for indicating that subsequent dependent operations are possible.


Do not use synchronize in Java without considering this unusual possibility.

public state getInstance() {

        if (instance==null) {
                synchronize(this)
                if (instance == null)
                        instance = newMySingleton()
        }        
        return instance;
}

The synchronize keyword keeps multiple threads from running through the outer if clause at the same time preventing the possibility of generating more than one singleton. The problem is, although no two threads can order up a singleton at once, a later thread can hit the if while the early newMySingleton() is still being built. Until the early singleton fully exists, the later thread will have access to the build command which, when called, will get you an error - a mysterious error since you already tried to protect your code against a similar kind of error with synchronize. In a way, you still end up with two incompatibles at the same time, same place.

This is already covered at DoubleCheckedLockingIsBroken, methinks -- KarlKnechtel

See also AtomicUnit.


EditText of this page (last edited November 28, 2006) or FindPage with title or text search