2016年6月5日 星期日

Asynctask vs Thread in Android

在撰寫Android時想要實作資料傳輸
資料傳輸目前有兩種做法
第一種是就是 Asynctask
另一種是 Thread

兩種雖然都是實作多工運算
但是用途還是有所不一樣

以下是網路搜尋結果 (Reference

For long-running or CPU-intensive tasks, there are basically two ways to do this: Java threads, and Android's native AsyncTask.
Neither one is necessarily better than the other, but knowing when to use each call is essential to leveraging the system's performance to your benefit.
Use AsyncTask for:
  1. Simple network operations which do not require downloading a lot of data
  2. Disk-bound tasks that might take more than a few milliseconds
Use Java threads for:
  1. Network operations which involve moderate to large amounts of data (either uploading or downloading)
  2. High-CPU tasks which need to be run in the background
  3. Any task where you want to control the CPU usage relative to the GUI thread

看來傳輸資料用選項一較為合適

若作為更新整個應用程式的話,我應該會選擇選項二,雖然是要下載更大資料(尚未實踐)

沒有留言:

張貼留言