Android Open Source - android-gskbyte-utils Queued Task Executor






From Project

Back to project page android-gskbyte-utils.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project android-gskbyte-utils listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.gskbyte.tasks;
/*ww  w  .ja va  2 s  . c  om*/
import java.util.ArrayList;
import java.util.List;

/**
 * Utility class to run groups of threads (represented by tasks) in an ordered basis.
 * 
 * The idea is to run a set of threads concurrently on every step, and once all threads in a step are finished,
 * execute the next one. Once all steps are finished, a callback method on a listener object is called.
 * */
public class QueuedTaskExecutor
{

/**
 * Listener with a callback method to be called when all steps are finished, or when there is an error
 * in one of the thread steps.
 * 
 * */
public interface Listener
{
    /**
     * Will be called when given two conditions:
     * - All threads in all steps have finished.
     * - A thread in a step didn't finish with success and the following steps requires the current to have success
     * in all threads.
     * 
     * @param executor The TaskCentral calling this method. You can call information methods to get info about the execution.
     * */
    public void queuedTaskExecutorFinished(QueuedTaskExecutor executor);
}

public enum Status
{
    PENDING, // waiting to be executed
    RUNNING, // threads are running
    FINISHED // execution completed
}

protected final ArrayList<TaskStep> steps = new ArrayList<TaskStep>();
protected final List<Exception> capturedExceptions = new ArrayList<Exception>();

protected int executedSteps = 0;
protected Listener listener = null;
protected boolean totalSuccessOnLastExecutedStep = false;
protected Status status = Status.PENDING;

public boolean run(Listener listener)
{
    if(status != Status.PENDING)
        throw new IllegalStateException("This " + getClass().getSimpleName() + " is running or already finished. It can be run only once.");
    this.listener = listener;
    if(steps.size() > 0) {
        steps.get(0).executeTasks();
    } else {
        notifyListener();
        return true;
    }
    
    return true;
}

/**
 * @return the number of steps
 * */
public synchronized int countSteps()
{ return steps.size(); }

/**
 * @return the umber of steps executed until the moment
 * */
public synchronized int countExecutedSteps()
{ return executedSteps; }

/**
 * @return true if all steps and all their tasks have been completed
 * */
public synchronized boolean didExecuteAllStepsWithSuccess()
{ return executedSteps == steps.size() && totalSuccessOnLastExecutedStep; }

/**
 * @return all captured exceptions in case of not success. Not having success doesn't neccesary mean that there are
 * exceptions.
 * */
public synchronized List<Exception> getCapturedExceptions()
{ return capturedExceptions;}

/**
 * @return the first captured exception, if any, or null if no exceptions are captured.
 * */
public synchronized Exception getFirstCapturedException()
{
    if(capturedExceptions.size()>0)
        return capturedExceptions.get( 0 );
    else
        return null;
}

/**
 * @return the last captured exception, if any, or null if no exceptions are captured.
 * */
public synchronized Exception getLastCapturedException()
{
    if(capturedExceptions.size()>0)
        return capturedExceptions.get( capturedExceptions.size()-1 );
    else
        return null;
}


/**
 * 
 * */
public synchronized void addStep(Task ... tasks)
{
    if(tasks.length == 0)
        throw new IllegalArgumentException("You must pass at least one StepRunnable");
    
    TaskStep step = new TaskStep(this, steps.size(), tasks);
    steps.add(step);
}

protected synchronized void stepFinished(int index, boolean totalSuccess, List<Exception> capturedExceptionsInStep)
{
    if(index != executedSteps)
        throw new IllegalStateException("Oh fuck fuck fuck!!!");
    ++executedSteps;
    totalSuccessOnLastExecutedStep = totalSuccess;
    
    if(capturedExceptionsInStep != null)
        this.capturedExceptions.addAll(capturedExceptionsInStep);
    
    if(index == steps.size()-1) { // last step?
        notifyListener();
    } else { 
        TaskStep nextStep = steps.get(index+1);
        if(!totalSuccess) {
            notifyListener();
        } else {
            nextStep.executeTasks();
        }
    }
    
}

protected void notifyListener()
{
    if(listener != null) {
        listener.queuedTaskExecutorFinished(QueuedTaskExecutor.this);
    }
}

}




Java Source Code List

com.woozzu.android.widget.IndexScroller.java
com.woozzu.android.widget.IndexableListView.java
org.gskbyte.FragmentWrapperActivity.java
org.gskbyte.animation.ExpandAnimation.java
org.gskbyte.bitmap.AbstractBitmapManager.java
org.gskbyte.bitmap.BitmapColorizer.java
org.gskbyte.bitmap.BitmapManager.java
org.gskbyte.bitmap.CachedBitmapColorizer.java
org.gskbyte.bitmap.IndexedBitmaps.java
org.gskbyte.bitmap.LRUBitmapCache.java
org.gskbyte.bitmap.LRUBitmapManager.java
org.gskbyte.bitmap.PrivateBitmapManager.java
org.gskbyte.bitmap.ReferencedBitmaps.java
org.gskbyte.collection.ArrayHashMap.java
org.gskbyte.collection.DoubleSparseArray.java
org.gskbyte.collection.ListHashMap.java
org.gskbyte.dialog.DownloadDialogFragment.java
org.gskbyte.dialog.LoadDialogFragment.java
org.gskbyte.dialog.OpenLinkDialogBuilder.java
org.gskbyte.dialog.PickerDialogFragment.java
org.gskbyte.download.DiskDownload.java
org.gskbyte.download.DownloadManager.java
org.gskbyte.download.Download.java
org.gskbyte.download.MemoryDownload.java
org.gskbyte.drawable.AutoBackgroundButtonDrawable.java
org.gskbyte.listener.IListenable.java
org.gskbyte.listener.ListenableNG.java
org.gskbyte.listener.Listenable.java
org.gskbyte.preferences.DialogSeekBarPreference.java
org.gskbyte.preferences.InlineSeekBarPreference.java
org.gskbyte.remote.AsyncURLRequest.java
org.gskbyte.remote.URLRequest.java
org.gskbyte.tasks.QueuedTaskExecutor.java
org.gskbyte.tasks.TaskStep.java
org.gskbyte.tasks.Task.java
org.gskbyte.ui.ArrayAdapterWithDefaultValue.java
org.gskbyte.ui.ListAdapter.java
org.gskbyte.ui.ColorDialog.ColorDialog.java
org.gskbyte.ui.ColorDialog.ColorPreference.java
org.gskbyte.ui.iconifiedMainMenuList.EntryView.java
org.gskbyte.ui.iconifiedMainMenuList.MainMenuAdapter.java
org.gskbyte.ui.iconifiedMainMenuList.MenuEntry.java
org.gskbyte.util.FrequentIntents.java
org.gskbyte.util.IOUtils.java
org.gskbyte.util.Logger.java
org.gskbyte.util.OpenFileHandlerFactory.java
org.gskbyte.util.OpenFileHandler.java
org.gskbyte.util.XmlUtils.java
org.gskbyte.view.AsyncImageView.java
org.gskbyte.view.AutoBackgroundButton.java
org.gskbyte.view.AutoBackgroundImageButton.java
org.gskbyte.view.AutoHeightImageView.java
org.gskbyte.view.ExpandedGridView.java
org.gskbyte.view.ExpandedListView.java
org.gskbyte.view.FontUtil.java
org.gskbyte.view.FontableButton.java
org.gskbyte.view.FontableCheckBox.java
org.gskbyte.view.FontableEditText.java
org.gskbyte.view.FontableTextView.java
org.gskbyte.view.FullWidthImageView.java
org.gskbyte.view.ProportionalHeightLayout.java
org.gskbyte.view.PullToRefreshListView.java
org.gskbyte.view.SquaredLayout.java
org.gskbyte.view.StepSeekBar.java
org.gskbyte.view.TextViewUtil.java
org.gskbyte.view.ViewUtils.java