AsyncTask中的doInBackground可能会导致竞争情况吗? [英] Can doInBackground in an AsyncTask potentially cause a race condition?
问题描述
从AsyncTask上的doInBackground方法返回到UI的结果是否有可能导致意外的竞争条件?使用'get'可以使UI等待计算返回结果,但这似乎违反了在后台运行任务的目标.
Does the return of a result from the doInBackground method on AsyncTask to the UI have the potential to cause unexpected race conditions? Using 'get' can make the UI wait for the computation to return the result, but that seems to defeat the objective on running a task in background.
相关文档:
https://developer.android.com/reference/android/os/AsyncTask.html
推荐答案
doInBackground在另一个线程上运行,因此,它当然会引起争用情况-每当您有两个线程,或者传入一个线程和异步事件时,您就可以拥有一个比赛条件.您始终必须编写代码来避免它们.但是,不会将值从doInBackground返回到onPostExecute,因此,您确实有一种安全的方式在线程之间传递数据而不会发生争用.
doInBackground runs on another thread, so of course it can cause a race condition- anytime you have two threads, or a thread and asynchronous events incoming you can have a race condition. You always have to code to avoid them. However the return of values from doInBackground to onPostExecute will not, so you do have a safe way of passing data between the threads without a race.
这篇关于AsyncTask中的doInBackground可能会导致竞争情况吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!