原始碼篇 AsyncTask機制

2021-07-09 03:27:48 字數 1690 閱讀 8689

asynctask

new asynctask()

// 執行在子執行緒中,做耗時操作

string doingbackground(string s)

// 執行在主線程中,耗時操作完成,更新ui

onpostexecute(string s)

}.execute(string);

asynctask的execute方法
public final asynctaskexecute(params... params) 

};mfuture = new futuretask(mworker)

};} // 把mworker傳遞給futuretask,callable指的就是mworker

public futuretask(callablecallable)

// 把mworker傳遞給sync,callable指的是mworker

sync(callablecallable)

執行緒池執行futuretask,就是執行futuretask的run方法,**如下:
public void run() 

void innerrun() catch (throwable ex)

// 轉調了sync.innerset(v);

set(result);

} else

}mworker = new workerrunnable()

};protected void set(v v)

void innerset(v v)

if (compareandsetstate(s, ran)) }}

mfuture = new futuretask(mworker) catch (interruptedexception e) catch (executionexception e) catch (cancellationexception e) catch (throwable t)

// 傳送了乙個message

message = shandler.obtainmessage(message_post_result,

new asynctaskresult(asynctask.this, result));

message.sendtotarget();}};

public v get() throws interruptedexception, executionexception

v innerget() throws interruptedexception, executionexception

在asynctask的成員變數,建立了internalhandler
private static class internalhandler extends handler )

@override

public void handlemessage(message msg)

}}

private static class asynctaskresult

}private void finish(result result)

原始碼篇 AsyncTask機制

asynctasknew asynctask 執行在子執行緒中,做耗時操作 string doingbackground string s 執行在主線程中,耗時操作完成,更新ui onpostexecute string s execute string asynctask的execute方法pub...

AsyncTask非同步任務機制原始碼分析

建立乙個自定義列表 如何建立乙個註腳 注釋也是必不可少的 katex數學公式 新的甘特圖功能,豐富你的文章 uml 圖表 flowchart流程圖 匯出與匯入 你好!這是你第一次使用markdown編輯器所展示的歡迎頁。如果你想學習如何使用markdown編輯器,可以仔細閱讀這篇文章,了解一下mar...

AsyncTask原始碼分析

就從asynctask的構造方法開始分析 creates a new asynchronous task.this constructor must be invoked on the ui thread.public asynctask mfuture new futuretask mworker...