Java 等待其他异步任务';在处理onPostExecute之前在AsyncTask中完成
我正在从一个URL获取信息,并将该信息发送到多个URL。我在Java 等待其他异步任务';在处理onPostExecute之前在AsyncTask中完成,java,android,android-asynctask,Java,Android,Android Asynctask,我正在从一个URL获取信息,并将该信息发送到多个URL。我在AsyncTask中的一个“for”上运行它,在第一个AsyncTask的onPostExecute上,我更改了一个TextView并告诉用户“一切都完成了” 但问题是,即使是AsyncTask的子级也在继续执行,父级AsyncTask执行其onPostExecute。所以我需要等他们,我被他们困住了 因此,我有两个AsyncTask类。这是我发送到网站的课程: class SendTo extends AsyncTask <Ob
AsyncTask
中的一个“for”上运行它,在第一个AsyncTask
的onPostExecute上,我更改了一个TextView
并告诉用户“一切都完成了”
但问题是,即使是AsyncTask
的子级也在继续执行,父级AsyncTask
执行其onPostExecute
。所以我需要等他们,我被他们困住了
因此,我有两个AsyncTask类。这是我发送到网站的课程:
class SendTo extends AsyncTask <Object,Object,String>{
@Override
protected String doInBackground(Object... strings) {
....
}
protected void onPostExecute(String s) {
super.onPostExecute(s);
//update ui for gathered information
}
}
class GetFrom extends AsyncTask <Object,Object,String>{
@Override
protected String doInBackground(Object... strings) {
....
String param = anotherMagicalFunctionThatGivesParamToSend(strings[1]);
for(i = 1; i < websites.length; i++){
publishProgress(websites[i],param);
}
return "";
}
protected void onProgressUpdate(final Object... values) {
super.onProgressUpdate(values);
new SendTo().executeOnExecutor(THREAD_POOL_EXECUTOR, values[1], values[0]);
}
protected void onPostExecute(String s) {
super.onPostExecute(s);
//finish things
}
}
类SendTo扩展异步任务{
@凌驾
受保护的字符串doInBackground(对象…字符串){
....
}
受保护的void onPostExecute(字符串s){
super.onPostExecute(s);
//更新收集信息的ui
}
}
这是我从网站上得到的任务:
class SendTo extends AsyncTask <Object,Object,String>{
@Override
protected String doInBackground(Object... strings) {
....
}
protected void onPostExecute(String s) {
super.onPostExecute(s);
//update ui for gathered information
}
}
class GetFrom extends AsyncTask <Object,Object,String>{
@Override
protected String doInBackground(Object... strings) {
....
String param = anotherMagicalFunctionThatGivesParamToSend(strings[1]);
for(i = 1; i < websites.length; i++){
publishProgress(websites[i],param);
}
return "";
}
protected void onProgressUpdate(final Object... values) {
super.onProgressUpdate(values);
new SendTo().executeOnExecutor(THREAD_POOL_EXECUTOR, values[1], values[0]);
}
protected void onPostExecute(String s) {
super.onPostExecute(s);
//finish things
}
}
class GetFrom扩展异步任务{
@凌驾
受保护的字符串doInBackground(对象…字符串){
....
String param=AnothermagicalFunction,提供SPARAMTOSend(字符串[1]);
对于(i=1;i<1.length;i++){
出版进度(网站[i],参数);
}
返回“”;
}
受保护的void onProgressUpdate(最终对象…值){
super.onProgressUpdate(值);
new SendTo().executeOnExecutor(线程池执行器,值[1],值[0]);
}
受保护的void onPostExecute(字符串s){
super.onPostExecute(s);
//完成事情
}
}
您可以在子asyncTask中传递TextView,并在子asyncTask的onPost方法中更新其值。您可以在子asyncTask中传递TextView,并在子asyncTask的onPost方法中更新其值
asyncTask
的postExecute()中运行以下for循环
,因为在线程
中运行线程
是没有用的
全局变量
,该变量将包含已完成的异步任务的计数
,当每个异步任务
完成执行时,该变量将递增1。i、 e它将进入postExecute
postExecute
中,检查该计数是否等于网站数。如果这个数字等于网站的数量,这基本上意味着你的所有数据都已成功同步,否则它仍在处理中asyncTask
的postExecute()中运行以下for循环
,因为在线程
中运行线程
是没有用的
全局变量
,该变量将包含已完成的异步任务的计数
,当每个异步任务
完成执行时,该变量将递增1。i、 e它将进入postExecute
postExecute
中,检查该计数是否等于网站数。如果这个数字等于网站的数量,这基本上意味着你的所有数据都已成功同步,否则它仍在处理中我建议在这种情况下使用回调 创建一个接口:
public interface MyCallback {
public void readycallback(int index_thread);
}
头等舱:
class SendTo extends AsyncTask <Object,Object,String>{
private MyCallback cb;
private int i;
public SendTo(int i, MyCallback cb) {
this.cb = cb;
this.i = i;
}
@Override
protected String doInBackground(Object... strings) {
....
}
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (cb != null)
cb.readycallback(i);
//update ui for gathered information
}
}
类SendTo扩展异步任务{
私人密室;
私人互联网i;
公共发送到(int i,MyCallback cb){
this.cb=cb;
这个。i=i;
}
@凌驾
受保护的字符串doInBackground(对象…字符串){
....
}
受保护的void onPostExecute(字符串s){
super.onPostExecute(s);
如果(cb!=null)
cb.readycallback(i);
//更新收集信息的ui
}
}
二等舱:
class GetFrom extends AsyncTask <Object,Object,String>{
private boolean[] array_of_completed_tasks = new boolean[websites.length - 1];
@Override
protected String doInBackground(Object... strings) {
....
String param = anotherMagicalFunctionThatGivesParamToSend(strings[1]);
for(i = 1; i < websites.length; i++){
publishProgress(websites[i],param);
}
while(!checkIfAllThreadAreCompleted()) {
//waiting all threads
//you can wait
try {
Thread.sleep(10L);// ten milliseconds
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "";
}
protected void onProgressUpdate(final Object... values) {
super.onProgressUpdate(values);
new SendTo(i, new MyCallback() {
@Override
public void readycallback(int index_thread) {
//this is your ready callback
array_of_completed_tasks[index_thread] = true;
}
}).executeOnExecutor(THREAD_POOL_EXECUTOR, values[1], values[0]);
}
}
private boolean checkIfAllThreadAreCompleted() {
boolean ok = true;
for(i = 0; i < array_of_completed_tasks.length; i++){
ok = ok && array_of_completed_tasks[i];
}
return ok;
protected void onPostExecute(String s) {
super.onPostExecute(s);
//finish things
}
}
class GetFrom扩展异步任务{
私有布尔值[]数组_of_completed_tasks=新布尔值[websites.length-1];
@凌驾
受保护的字符串doInBackground(对象…字符串){
....
String param=AnothermagicalFunction,提供SPARAMTOSend(字符串[1]);
对于(i=1;i<1.length;i++){
出版进度(网站[i],参数);
}
而(!checkIfAllThreadAreCompleted()){
//等待所有线程
//你可以等
试一试{
Thread.sleep(10L);//十毫秒
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
返回“”;
}
受保护的void onProgressUpdate(最终对象…值){
super.onProgressUpdate(值);
新发送到(i,新的MyCallback(){
@凌驾
public void readycallback(int index_线程){
//这是你的回电
已完成任务的数组\u[索引\u线程]=真;
}
}).executeOnExecutor(线程池执行器,值[1],值[0]);
}
}
私有布尔检查全部线程完成(){
布尔ok=true;
对于(i=0;i
或者使用线程来获得更好的编码风格。但我真的认为使用
AsyncTask
是非常难看的方法,因为它是真正的项目。我建议在这种情况下使用回调
创建一个接口:
public interface MyCallback {
public void readycallback(int index_thread);
}
头等舱:
class SendTo extends AsyncTask <Object,Object,String>{
private MyCallback cb;
private int i;
public SendTo(int i, MyCallback cb) {
this.cb = cb;
this.i = i;
}
@Override
protected String doInBackground(Object... strings) {
....
}
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (cb != null)
cb.readycallback(i);
//update ui for gathered information
}
}
类SendTo扩展异步任务{
私人密室;
私人互联网i;
公共发送到(int i,MyCallback cb){
this.cb=cb;
这个。i=i;