安卓線程 總結

參考 https://blog.csdn.net/qq_22393017/article/details/79356115

5、android 創建線程的三種方式

一、繼承Thread類創建線程類

(1)定義Thread類的子類,並重寫該類的run方法,該run方法的方法體就代表了線程要完成的任務。因此把run()方法稱爲執行體。

(2)創建Thread子類的實例,即創建了線程對象。

(3)調用線程對象的start()方法來啓動該線程。

二、通過Runnable接口創建線程類

 

(1)定義runnable接口的實現類,並重寫該接口的run()方法,該run()方法的方法體同樣是該線程的線程執行體。

 

(2)創建 Runnable實現類的實例,並依此實例作爲Thread的target來創建Thread對象,該Thread對象纔是真正的線程對象。

 

(3)調用線程對象的start()方法來啓動該線程。

三、通過Callable和Future創建線程

 

(1)創建Callable接口的實現類,並實現call()方法,該call()方法將作爲線程執行體,並且有返回值。

 

(2)創建Callable實現類的實例,使用FutureTask類來包裝Callable對象,該FutureTask對象封裝了該Callable對象的call()方法的返回值。

 

(3)使用FutureTask對象作爲Thread對象的target創建並啓動新線程。

 

(4)調用FutureTask對象的get()方法來獲得子線程執行結束後的返回值

6、線程池ThreadPoolExecutor

一、ThreadPoolExecutor mExecutor = new ThreadPoolExecutor(//

mCorePoolSize, // 核心線程數

mMaximumPoolSize,// 最大線程數

mKeepAliveTime,// 保持時間

unit,// 保持時間的單位

workQueue,// 工作隊列

threadFactory,// 線程工廠

handler// 異常捕獲器

);

/**提交任務*/

public Future<?> submit(Runnable task) {

initThreadPoolExecutor();

Future<?> submit = mExecutor.submit(task);

return submit;

}

 

/**執行任務*/

public void execute(Runnable task) {

initThreadPoolExecutor();

mExecutor.execute(task);

}

 

/**移除任務*/

public void remove(Runnable task) {

initThreadPoolExecutor();

mExecutor.remove(task);

}

//關閉線程池

public void shutUpNow(){

if (mExecutor != null){

mExecutor.shutdownNow();

}

}

二、newCachedThreadPool

private ExecutorService service = null;

private CacheRunable cacheRunable;

private Future<?> cacheRunableFuture;

/**

* 開始緩衝處理線程

*/

private void startCacheRunner() {

 

if (service != null) {

service.shutdownNow();

service = null;

}

 

service = Executors.newCachedThreadPool();

//開始接收線程

if (cacheRunable == null) {

cacheRunable = new CacheRunable();

}

cacheRunableFuture = service.submit(cacheRunable);

}

if (service != null) {

service.shutdownNow();

service = null;

}

if (cacheRunableFuture != null) {

cacheRunableFuture.cancel(true);

}

/**

* @Description 緩衝區處理線程

*/

private class CacheRunable implements Runnable {

三、newFixedThreadPool

ExecutorService executorService = Executors.newFixedThreadPool(2);

executorService.submit(new Runnable() {

@Override

public void run() {

}

});

四、newSingleThreadExecutor

ExecutorService executorService = Executors.newSingleThreadExecutor();

executorService.submit(new Runnable() {

@Override

public void run() {

 

}

});

五、ScheduledExecutorService

ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);

Runnable runnable = new Runnable(){

@Override

public void run() {

//TODO method();

}

};

//延遲一秒執行

scheduledExecutorService.schedule(runnable, 1, TimeUnit.SECONDS);

 

ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);

Runnable runnable = new Runnable(){

@Override

public void run() {

//TODO method();

}

};

//延遲三秒後,執行週期一秒的定時任務

scheduledExecutorService.scheduleAtFixedRate(runnable, 3, 1, TimeUnit.SECONDS);

 

六、如何終止線程池中的某個線程任務?

一般線程執行完run方法之後,線程就正常結束了,因此有如下幾種方式來實現:

 

4.1 利用 Future 和 Callable。

步驟:

 

實現 Callable 接口

調用 pool.submit() 方法,返回 Future 對象

用 Future 對象來獲取線程的狀態。

 

private void cancelAThread() {

ExecutorService pool = Executors.newFixedThreadPool(2);

Callable<String> callable = new Callable<String>() {

@Override

public String call() throws Exception {

System.out.println("test");

return "true";

}

};

Future<String> f = pool.submit(callable);

System.out.println(f.isCancelled());

System.out.println(f.isDone());

f.cancel(true);

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章