FileDocCategorySizeDatePackage
MutexFileProvider.javaAPI DocAndroid 5.1 API3542Thu Mar 12 22:22:42 GMT 2015com.android.printspooler.model

MutexFileProvider

public final class MutexFileProvider extends Object
This class provides a shared file to several threads. Only one thread at a time can use the file. To acquire the file a thread has to request it in a blocking call to {@link #acquireFile(OnReleaseRequestCallback)}. The provided callback is optional and is used to notify the owning thread when another one wants to acquire the file. In case a release is requested the thread owning the file must release it as soon as possible. If no callback is provided a thread that acquires the file must release it as soon as possible, i.e. even if callback was provided the thread cannot have the file for less time.

Fields Summary
private static final String
LOG_TAG
private static final boolean
DEBUG
private final Object
mLock
private final File
mFile
private Thread
mOwnerThread
private OnReleaseRequestCallback
mOnReleaseRequestCallback
Constructors Summary
public MutexFileProvider(File file)


       
           
    

         
        mFile = file;
        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();
    
Methods Summary
public java.io.FileacquireFile(com.android.printspooler.model.MutexFileProvider$OnReleaseRequestCallback callback)

        synchronized (mLock) {
            // If this thread has the file, nothing to do.
            if (mOwnerThread == Thread.currentThread()) {
                return mFile;
            }

            // Another thread wants file ask for a release.
            if (mOwnerThread != null && mOnReleaseRequestCallback != null) {
                mOnReleaseRequestCallback.onReleaseRequested(mFile);
            }

            // Wait until the file is released.
            while (mOwnerThread != null) {
                try {
                    mLock.wait();
                } catch (InterruptedException ie) {
                    /* ignore */
                }
            }

            // Update the owner and the callback.
            mOwnerThread = Thread.currentThread();
            mOnReleaseRequestCallback = callback;

            if (DEBUG) {
                Log.i(LOG_TAG, "Acquired file: " + mFile + " by thread: " + mOwnerThread);
            }

            return mFile;
        }
    
public voidreleaseFile()

        synchronized (mLock) {
            if (mOwnerThread != Thread.currentThread()) {
                return;
            }

            if (DEBUG) {
                Log.i(LOG_TAG, "Released file: " + mFile + " from thread: " + mOwnerThread);
            }

            // Update the owner and the callback.
            mOwnerThread = null;
            mOnReleaseRequestCallback = null;

            mLock.notifyAll();
        }