private synchronized long getUniqueTimeStamp() {

        //Create a default time stamp:
        long timeStamp = System.currentTimeMillis();

        //Lock file for ensuring we create a unique time stamp for all
        //processes on the system:
        File lockFile = null;

        try {

            lockFile = new File(TEMPORARY_DIRECTORY, "guid.lock");

            //Last modified time stamp for the lock file:
            long lastModified = fileLastModified(lockFile);

            //Maximum absolute time stamp to execute the lock file algorithm:
            //NOTE: This upper bound (1000 milliseconds or 1 second) provides an escape mechanism for the
            //scenario where a previous instance of the JVM crashed and a stray lock file persists on the 
            //local file system.
            long maxWaitTimeStamp = (System.currentTimeMillis() + 1000);

            //Loop until we can create the lock file (e.g. exclusive rights)
            //since creating a file is atomic or the maximum absolute time 
            //stamp to execute the lock file algorithm has expired:
            while (true) {

                try {

                    //After the lock file has been created, we have exclusive
                    //rights to the lock file and enter the process 
                    //synchronized block:
                    if (lockFile.createNewFile()) {
                        break;
                    }

                    //If we cannot create the lock file, only continue until
                    //the maximum absolute time stamp to execute the lock 
                    //file algorithm has expired:
                    else if (System.currentTimeMillis() > maxWaitTimeStamp) {

                        //If the lock file has not been updated before the
                        //maximum absolute time stamp to execute the lock 
                        //file algorithm has expired, delete the lock file 
                        //and attempt to execute the lock algorithm again:
                        if (fileLastModified(lockFile) <= lastModified) {

                            delete(lockFile);

                            //Only permit this instance of the algorithm to
                            //attempt to delete the lock file once:
                            lastModified = -1;
                        }

                        //If a lock cannot be obtained before the maximum
                        //absolute time stamp to execute the lock file 
                        //algorithm has expired, return the non-atomic 
                        //current time stamp:
                        else {
                            return timeStamp;
                        }
                    }
                } catch (IOException i) {

                    //Return the non-atomic current time stamp since the 
                    //lock file could not be created.
                    return timeStamp;
                }
            }

            //Wait 1/10 second (100 milliseconds) to ensure a unique time
            //stamp:
            try {
                Thread.sleep(100);
            } catch (InterruptedException i) {
                //Ignore since we are only sleeping.
            }

            //Create a unique time stamp as an atomic operation:
            timeStamp = System.currentTimeMillis();
        } catch (SecurityException s) {
            //Ignore all security exceptions and exit gracefully.
        } finally {

            try {

                //After the lock file has been atomically deleted, we release 
                //the exclusive rights to the lock file exit the process
                //synchronized block:
                if (lockFile != null) {
                    delete(lockFile);
                }
            } catch (SecurityException s) {
                //Ignore all security exceptions and exit gracefully.
            }
        }

        return timeStamp;
    }
private synchronized long getUniqueTimeStamp ( ) { long timeStamp = System . currentTimeMillis ( ) ; File lockFile = null ; try { lockFile = new File ( TEMPORARY_DIRECTORY , "guid.lock" ) ; long lastModified = fileLastModified ( lockFile ) ; long maxWaitTimeStamp = ( System . currentTimeMillis ( ) + 1000 ) ; while ( true ) { try { if ( lockFile . createNewFile ( ) ) { break ; } else if ( System . currentTimeMillis ( ) > maxWaitTimeStamp ) { if ( fileLastModified ( lockFile ) <= lastModified ) { delete ( lockFile ) ; lastModified = - 1 ; } else { return timeStamp ; } } } catch ( IOException i ) { return timeStamp ; } } try { Thread . sleep ( 100 ) ; } catch ( InterruptedException i ) { } timeStamp = System . currentTimeMillis ( ) ; } catch ( SecurityException s ) { } finally { try { if ( lockFile != null ) { delete ( lockFile ) ; } } catch ( SecurityException s ) { } } return timeStamp ; }
Returns an unique current time stamp.
<p>
Since the a millisecond time stamp is the most granular unit of time
in Java and multiple time stamps may be generated within the one millisecond,
cross-thread and cross-process (e.g. JVM) synchronization is required.
<p>
Threading locking is used to ensure the atomic generation of unique time stamps
between threads. File locking (only truly atomic operation in Java) us used to
ensure the atomic generation of unique time stamps between processes (e.g. JVM)
on the same host.
<p>

@return Unique current time stamp.