FileDocCategorySizeDatePackage
QueuedWork.javaAPI DocAndroid 5.1 API3601Thu Mar 12 22:22:10 GMT 2015android.app

QueuedWork

public class QueuedWork extends Object
Internal utility class to keep track of process-global work that's outstanding and hasn't been finished yet. This was created for writing SharedPreference edits out asynchronously so we'd have a mechanism to wait for the writes in Activity.onPause and similar places, but we may use this mechanism for other things in the future.
hide

Fields Summary
private static final ConcurrentLinkedQueue
sPendingWorkFinishers
private static ExecutorService
sSingleThreadExecutor
Constructors Summary
Methods Summary
public static voidadd(java.lang.Runnable finisher)
Add a runnable to finish (or wait for) a deferred operation started in this context earlier. Typically finished by e.g. an Activity#onPause. Used by SharedPreferences$Editor#startCommit(). Note that this doesn't actually start it running. This is just a scratch set for callers doing async work to keep updated with what's in-flight. In the common case, caller code (e.g. SharedPreferences) will pretty quickly call remove() after an add(). The only time these Runnables are run is from waitToFinish(), below.

        sPendingWorkFinishers.add(finisher);
    
public static booleanhasPendingWork()
Returns true if there is pending work to be done. Note that the result is out of data as soon as you receive it, so be careful how you use it.

        return !sPendingWorkFinishers.isEmpty();
    
public static voidremove(java.lang.Runnable finisher)

        sPendingWorkFinishers.remove(finisher);
    
public static java.util.concurrent.ExecutorServicesingleThreadExecutor()
Returns a single-thread Executor shared by the entire process, creating it if necessary.

 // lazy, guarded by class

                      
        
        synchronized (QueuedWork.class) {
            if (sSingleThreadExecutor == null) {
                // TODO: can we give this single thread a thread name?
                sSingleThreadExecutor = Executors.newSingleThreadExecutor();
            }
            return sSingleThreadExecutor;
        }
    
public static voidwaitToFinish()
Finishes or waits for async operations to complete. (e.g. SharedPreferences$Editor#startCommit writes) Is called from the Activity base class's onPause(), after BroadcastReceiver's onReceive, after Service command handling, etc. (so async work is never lost)

        Runnable toFinish;
        while ((toFinish = sPendingWorkFinishers.poll()) != null) {
            toFinish.run();
        }