Android 台灣中文網
標題:
Needle 多線程的Library
[打印本頁]
作者:
ploglin
時間:
2016-3-24 10:48
標題:
Needle 多線程的Library
開發工具:Android Studio
Needle 是我在做機器人控制找到的套件,以往在多線程的控制,大部份都是用 AsyncTask 或 Handle 來控制,但要真的做到比較好的控制,代碼就越來越多,越來越多......。Needle提供了很便利的使用方式,我覺得非常的方便、快速,就推薦給大家吧。
首先在 Gradle 中加入
compile "com.zsoltsafrany:needle:1.0.0"
複製代碼
最基本的使用方式,是利用主線程
Needle.onMainThread().execute(new Runnable() {
@Override
public void run() {
// e.g. change one of the views
}
});
複製代碼
如果要多線程,就必需使用背景模式
Needle.onBackgroundThread().execute(new Runnable() {
@Override
public void run() {
// something cpu-intensive and/or not UI-related
}
});
複製代碼
還可以指定線程的數量唷
Needle.onBackgroundThread().withThreadPoolSize(6).execute(new Runnable() {
@Override
public void run() {
// something that blocks often thus can have a larger thread pool,
// for instance, downloading a file (with blocking IO)
}
});
複製代碼
還不錯吧,是不是簡單又方便呢?
歡迎光臨 Android 台灣中文網 (https://apk.tw/)
Powered by Discuz! X3.1