();
36 |
37 | private static final ThreadFactory sThreadFactory = new ThreadFactory() {
38 | private final AtomicInteger mCount = new AtomicInteger(1);
39 |
40 | public Thread newThread(Runnable r) {
41 |
42 | return new Thread(r, "AsyncTaskScheduler Thread#" + mCount.getAndIncrement());
43 | }
44 | };
45 |
46 | public static final Executor CACHED_THREAD_POOL = Executors.newCachedThreadPool(sThreadFactory);
47 |
48 | private Executor mDefaultPoolExecutor ;
49 |
50 |
51 | /**
52 | * set an {@link Executor} that can be used to execute tasks in parallel as default .
53 | */
54 | public AsyncTaskScheduler() {
55 | this.mDefaultPoolExecutor = CACHED_THREAD_POOL;
56 | }
57 |
58 | /**
59 | * you set an {@link Executor} that you what as default .
60 | */
61 | public AsyncTaskScheduler(Executor defaultPoolExecutor ) {
62 | this.mDefaultPoolExecutor = defaultPoolExecutor;
63 | }
64 |
65 | public AsyncTaskScheduler execute(@NonNull SingleAsyncTask singleAsyncTask){
66 | mDefaultPoolExecutor.execute(singleAsyncTask.getFutureTask());
67 | mSingleAsyncTaskList.add(singleAsyncTask);
68 | return this;
69 | }
70 |
71 |
72 | /**
73 | cancel a singleAsyncTask
74 | */
75 | public boolean cancelTask(SingleAsyncTask singleAsyncTask, boolean mayInterruptIfRunning) {
76 | return singleAsyncTask.cancel(mayInterruptIfRunning);
77 | }
78 |
79 | /**
80 | cancel all singleTask in the scheduler
81 | */
82 | public void cancelAllTasks(boolean mayInterruptIfRunning){
83 | for(SingleAsyncTask singleAsyncTask : mSingleAsyncTaskList) {
84 | cancelTask(singleAsyncTask,mayInterruptIfRunning);
85 | }
86 | mSingleAsyncTaskList.clear();
87 | }
88 |
89 | // public void shutDown() {
90 | // mDefaultPoolExecutor.shutdownNow();
91 | // synchronized (this) {
92 | // cancelAllTasks(true);
93 | // }
94 | // }
95 | //
96 | // public boolean isShutDown() {
97 | // return mDefaultPoolExecutor.isShutdown();
98 | // }
99 | }
100 |
101 |
--------------------------------------------------------------------------------
/library/src/main/java/com/silencedut/asynctaskscheduler/SingleAsyncTask.java:
--------------------------------------------------------------------------------
1 | package com.silencedut.asynctaskscheduler;
2 |
3 | import android.os.Binder;
4 | import android.os.Handler;
5 | import android.os.Looper;
6 | import android.os.Message;
7 | import android.support.annotation.MainThread;
8 | import android.support.annotation.WorkerThread;
9 |
10 | import java.util.concurrent.Callable;
11 | import java.util.concurrent.FutureTask;
12 | import java.util.concurrent.atomic.AtomicBoolean;
13 |
14 | /**
15 | *
16 | *
17 | * Created by SilenceDut on 16/7/21.
18 | *
19 | *
20 | * SingleTaskTask enables proper and easy use of the UI thread. This class allows to
21 | * perform background{@link #doInBackground} operations and publish results
22 | * {@link #onExecuteSucceed} on the UI thread without having to
23 | * manipulate threads and/or handlers.
24 | *
25 | * Usage
26 | * SingleTaskTask is a abstract class ,must be subclassed to be used. The subclass
27 | * will override at least one method ({@link #doInBackground}), and most often will
28 | * override a second one ({@link #onExecuteSucceed}.),use {@link #cancel} to cancel a
29 | * task ,but the task may be stop at once , however {@link #onExecuteSucceed} will not
30 | * be invoked,{@link #onExecuteCancelled(Object)} will be invoked.
31 | *
32 | * you should call {@link #cancel} when on some life cycle
33 | * such as {@link onDestroy} avoid memory leak
34 | *
35 | */
36 |
37 |
38 |
39 | public abstract class SingleAsyncTask