public void SetSynchronizer(TimestampSynchronizer sync) { TimestampSynchronizer old = mSync; if (old != null) { old.Deactivate(); } mSync = sync; /* Ok, now; synchronizer can tell us what is the first timestamp * value that definitely was NOT used by the previous incarnation. * This can serve as the last used time stamp, assuming it is not * less than value we are using now. */ if (sync != null) { long lastSaved = sync.Initialize(); if (lastSaved > mLastUsedTimestamp) { mLastUsedTimestamp = lastSaved; } } /* Also, we need to make sure there are now no safe values (since * synchronizer is not yet requested to allocate any): */ mFirstUnsafeTimestamp = 0L; // ie. will always trigger sync.update() }
/** * Method that can (and should) be called once right after getting * the instance, to ensure that system time stamp values used are * valid (with respect to values used earlier by JUG instances), and * to use file-lock based synchronization mechanism to prevent multiple * JVMs from running conflicting instances of JUG (first one to be * started wins on contention). It can also be called to stop * synchronization by calling it with argument null, although such * usage is strongly discouraged (ie. it's a good idea to either never * use synchronization, or always; but not to mix modes). *<p> * Caller needs to instantiate an instance of * {@link TimestampSynchronizer}; currently the only standard * implementation is * {@link org.safehaus.uuid.ext.FileBasedTimestampSynchronizer} (which * is JDK 1.4+). *<p> * Note: since the generator instance is a singleton, calling this * method will always cause all generation to be synchronized using * the specified method. * * @param sync Synchronizer instance to use for synchronization. */ public void SynchronizeExternally(TimestampSynchronizer sync) { lock (mTimerLock) { if (mTimer == null) { mTimer = new UUIDTimer(GetRandomNumberGenerator()); } mTimer.SetSynchronizer(sync); } }