EasyThread經過對原生的線程池進行封裝,可以讓你更方便的進行線程任務操做。java
點此前往GITHUB頁git
// 添加jitPack倉庫使用
maven { url 'https://jitpack.io' }
// 添加依賴
compile "com.github.yjfnypeu:EasyThread:0.5.0"
複製代碼
使用方式分兩步走:github
EasyThread easyThread = EasyThread.Builder
//提供了四種create方法,用於根據須要建立不一樣類型的線程池進行使用
//好比createSingle():表示建立一個單例的線程池進行使用
.createXXX()
.build();
複製代碼
EasyThread支持執行四種任務:安全
easyThread.execute(new Runnable(){
@Override
public void run() {
// do something.
}
});
複製代碼
Future task = easyThread.submit(new Callback<User>(){
@Override
public User call() throws Exception {
// do something
return user;
}
})
User result = task.get();
複製代碼
// 異步執行任務
Callable<User> callable = new Callable<User>(){
@Override
public User call() throws Exception {
// do something
return user;
}
}
// 異步回調
AsyncCallback<User> async = new AsyncCallback<User>() {
@Override
public void onSuccess(User user) {
// notify success;
}
@Override
public void onFailed(Throwable t) {
// notify failed.
}
};
// 啓動異步任務
easyThread.async(callable, async)
複製代碼
// 在啓動任務前,調用delay方法,指定延遲時間便可
easyThread.setDelay(time, unit)
.execute(runnable);
複製代碼
e.g 延遲3秒啓動執行任務bash
easyThread.setDelay(3, TimeUnit.SECONDS)
.execute(task);
複製代碼
EasyThread提供了各類的額外配置,經過這些配置可讓線程操做使用起來更加駕輕就熟。app
這裏咱們以配置後臺優先級進行說明:異步
1. 配置默認線程任務優先級(默認配置)async
EasyThread.Builder.createXXX().setPriority(priority);
複製代碼
2. 配置當前線程任務優先級(當前任務配置)maven
easyThread.setPriority(priority).execute(task);
複製代碼
配置方式:ide
easyThread.setName(name)// 配置線程任務名
.setPriority()// 配置線程運行優先級
複製代碼
接口說明:
public interface Callback {
// 線程任務啓動時的通知
void onStart (String threadName);
// 線程任務運行時出現異常時的通知
void onError (String threadName, Throwable t);
// 線程任務正常執行完成時的通知
void onCompleted (String threadName);
}
複製代碼
配置方式:
easyThread.setCallback(callback);
複製代碼
消息派發器用於消息回調線程切換,即指定回調任務須要運行在什麼線程之上。
好比說在Android平臺,很常見的就是回調時須要進行界面通知,因此這個時候就須要回調通知運行在UI線,便於操做。
配置方式:
// 派發器的實例類型爲java.util.concurrent.Executor子類
easyThread.setDeliver(deliver);
複製代碼
在默認條件下(即未配置額外的派發器時),在Android或者Java平臺,分別適配了不一樣的回調派發邏輯:
對於APP來講。線程資源是寶貴的。爲了不建立過多額外的線程,因此建議對每一個app。提供一個統一的管理器維護全部的線程池,以下所示:
public final class ThreadManager {
private final static EasyThread io;
private final static EasyThread cache;
private final static EasyThread calculator;
private final static EasyThread file;
public static EasyThread getIO () {
return io;
}
public static EasyThread getCache() {
return cache;
}
public static EasyThread getCalculator() {
return calculator;
}
public static EasyThread getFile() {
return file;
}
static {
io = EasyThread.Builder.createFixed(6).setName("IO").setPriority(7).setCallback(new DefaultCallback()).build();
cache = EasyThread.Builder.createCacheable().setName("cache").setCallback(new DefaultCallback()).build();
calculator = EasyThread.Builder.createFixed(4).setName("calculator").setPriority(Thread.MAX_PRIORITY).setCallback(new DefaultCallback()).build();
file = EasyThread.Builder.createFixed(4).setName("file").setPriority(3).setCallback(new DefaultCallback()).build();
}
private static class DefaultCallback implements Callback {
@Override
public void onError(String threadName, Throwable t) {
MyLog.e("Task with thread %s has occurs an error: %s", threadName, t.getMessage());
}
@Override
public void onCompleted(String threadName) {
MyLog.d("Task with thread %s completed", threadName);
}
@Override
public void onStart(String threadName) {
MyLog.d("Task with thread %s start running!", threadName);
}
}
}
複製代碼