Android中网络请求创建单个线程池的方法
生活随笔
收集整理的這篇文章主要介紹了
Android中网络请求创建单个线程池的方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
創建單個線程池的方法
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future;public class YhshThreadPoolFactory {private ExecutorService mThreadService = null;private YhshThreadPoolFactory() {mThreadService = Executors.newSingleThreadExecutor();}private static class YhshThreadPoolFactoryHolder {private static final YhshThreadPoolFactory INSTANCE = new YhshThreadPoolFactory();}public static YhshThreadPoolFactory getInstance() {return YhshThreadPoolFactoryHolder.INSTANCE;}public <T> Future<T> submitRequest(Runnable runnable, T result) {return mThreadService.submit(runnable, result);}public void executeRequest(Runnable runnable) {mThreadService.execute(runnable);}}?
下面是調用線程的方法,網絡請求寫在run里面即可
?
YhshThreadPoolFactory.getInstance().executeRequest(new Runnable() {@Overridepublic void run() {//寫相應的網絡請求邏輯即可}});?
總結
以上是生活随笔為你收集整理的Android中网络请求创建单个线程池的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python怎么制作游戏脚本
- 下一篇: Android中TextView中str