Skip to content

Commit

Permalink
update PriorityAsyncTask
Browse files Browse the repository at this point in the history
  • Loading branch information
wyouflf committed May 24, 2014
1 parent 9d2cb55 commit 94d11ca
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 60 deletions.
2 changes: 1 addition & 1 deletion library/src/com/lidroid/xutils/BitmapUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public <T extends View> void display(T container, String uri, BitmapDisplayConfi

Priority priority = displayConfig.getPriority();
if (priority == null) {
priority = Priority.UI_LOW;
priority = Priority.DEFAULT;
}
loadTask.executeOnExecutor(executor, priority);
}
Expand Down
2 changes: 1 addition & 1 deletion library/src/com/lidroid/xutils/HttpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ private <T> HttpHandler<T> sendRequest(HttpRequest request, RequestParams params
priority = params.getPriority();
}
if (priority == null) {
priority = Priority.UI_LOW;
priority = Priority.DEFAULT;
}
handler.executeOnExecutor(EXECUTOR, priority, request);
return handler;
Expand Down
10 changes: 6 additions & 4 deletions library/src/com/lidroid/xutils/task/Priority.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Time: 上午11:25
*/
public enum Priority {
UI_TOP(0), UI_NORMAL(1), UI_LOW(2), BG_TOP(3), BG_NORMAL(4), BG_LOW(5);
UI_TOP(0), UI_NORMAL(1), UI_LOW(2), DEFAULT(3), BG_TOP(4), BG_NORMAL(5), BG_LOW(6);
private int value = 0;

Priority(int value) {
Expand All @@ -26,13 +26,15 @@ public static Priority valueOf(int value) {
case 2:
return UI_LOW;
case 3:
return BG_TOP;
return DEFAULT;
case 4:
return BG_NORMAL;
return BG_TOP;
case 5:
return BG_NORMAL;
case 6:
return BG_LOW;
default:
return UI_LOW;
return DEFAULT;
}
}
}
64 changes: 11 additions & 53 deletions library/src/com/lidroid/xutils/task/PriorityAsyncTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,11 @@ public abstract class PriorityAsyncTask<Params, Progress, Result> implements Tas
private final WorkerRunnable<Params, Result> mWorker;
private final FutureTask<Result> mFuture;

private volatile Status mStatus = Status.PENDING;
private volatile boolean mExecuteInvoked = false;

private final AtomicBoolean mCancelled = new AtomicBoolean();
private final AtomicBoolean mTaskInvoked = new AtomicBoolean();

/**
* Indicates the current status of the task. Each status will be set only once
* during the lifetime of a task.
*/
public enum Status {
/**
* Indicates that the task has not been executed yet.
*/
PENDING,
/**
* Indicates that the task is running.
*/
RUNNING,
/**
* Indicates that {@link PriorityAsyncTask#onPostExecute} has finished.
*/
FINISHED,
}

/**
* Creates a new asynchronous task. This constructor must be invoked on the UI thread.
*/
Expand Down Expand Up @@ -110,15 +91,6 @@ private Result postResult(Result result) {
return result;
}

/**
* Returns the current status of this task.
*
* @return The current status.
*/
public final Status getStatus() {
return mStatus;
}

/**
* Override this method to perform a computation on a background thread. The
* specified parameters are the parameters passed to {@link #execute}
Expand Down Expand Up @@ -300,8 +272,7 @@ public final Result get(long timeout, TimeUnit unit) throws InterruptedException
/**
* @param params The parameters of the task.
* @return This instance of AsyncTask.
* @throws IllegalStateException If {@link #getStatus()} returns either
* {@link PriorityAsyncTask.Status#RUNNING} or {@link PriorityAsyncTask.Status#FINISHED}.
* @throws IllegalStateException If execute has invoked.
* @see #executeOnExecutor(java.util.concurrent.Executor, Object[])
* @see #execute(Runnable)
*/
Expand All @@ -313,8 +284,7 @@ public final PriorityAsyncTask<Params, Progress, Result> execute(Params... param
* @param priority
* @param params The parameters of the task.
* @return This instance of AsyncTask.
* @throws IllegalStateException If {@link #getStatus()} returns either
* {@link PriorityAsyncTask.Status#RUNNING} or {@link PriorityAsyncTask.Status#FINISHED}.
* @throws IllegalStateException If execute has invoked.
* @see #executeOnExecutor(java.util.concurrent.Executor, Object[])
* @see #execute(Runnable)
*/
Expand All @@ -326,42 +296,31 @@ public final PriorityAsyncTask<Params, Progress, Result> execute(Priority priori
* @param exec The executor to use.
* @param params The parameters of the task.
* @return This instance of AsyncTask.
* @throws IllegalStateException If {@link #getStatus()} returns either
* {@link PriorityAsyncTask.Status#RUNNING} or {@link PriorityAsyncTask.Status#FINISHED}.
* @throws IllegalStateException If execute has invoked.
* @see #execute(Object[])
*/
public final PriorityAsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec,
Params... params) {
return executeOnExecutor(exec, Priority.UI_LOW, params);
return executeOnExecutor(exec, Priority.DEFAULT, params);
}

/**
* @param exec The executor to use.
* @param priority
* @param params The parameters of the task.
* @return This instance of AsyncTask.
* @throws IllegalStateException If {@link #getStatus()} returns either
* {@link PriorityAsyncTask.Status#RUNNING} or {@link PriorityAsyncTask.Status#FINISHED}.
* @throws IllegalStateException If execute has invoked.
* @see #execute(Object[])
*/
public final PriorityAsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec,
Priority priority,
Params... params) {
if (mStatus != Status.PENDING) {
switch (mStatus) {
case RUNNING:
throw new IllegalStateException("Cannot execute task:"
+ " the task is already running.");
case FINISHED:
throw new IllegalStateException("Cannot execute task:"
+ " the task has already been executed "
+ "(a task can be executed only once)");
default:
break;
}
if (mExecuteInvoked) {
throw new IllegalStateException("Cannot execute task:"
+ " the task is already executed.");
}

mStatus = Status.RUNNING;
mExecuteInvoked = true;

onPreExecute();

Expand All @@ -380,7 +339,7 @@ public final PriorityAsyncTask<Params, Progress, Result> executeOnExecutor(Execu
* @see #executeOnExecutor(java.util.concurrent.Executor, Object[])
*/
public static void execute(Runnable runnable) {
execute(runnable, Priority.UI_LOW);
execute(runnable, Priority.DEFAULT);
}

/**
Expand Down Expand Up @@ -421,7 +380,6 @@ private void finish(Result result) {
} else {
onPostExecute(result);
}
mStatus = Status.FINISHED;
}

private static class InternalHandler extends Handler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ public void setValue(T value) {
this.value = (PriorityObject<?>) value;
this.valueAsT = true;
} else {
this.value = new PriorityObject<T>(Priority.UI_LOW, value);
this.value = new PriorityObject<T>(Priority.DEFAULT, value);
}
}
}

0 comments on commit 94d11ca

Please sign in to comment.