如何在循环语句中使线程同步?
我是线程新手。我正在尝试加载一小组照片。现在我正在使用异步任务/线程,但如何使结果顺序化?下面是一个例子:
我想要什么:
a[0] = photo1;
a[1] = photo2;
a[2] = photo3;
a[3] = photo4
我的程序给了我什么。请注意,顺序会发生变化并且是随机的:
a[0] = photo[2];
a[1] = photo[1];
a[2] = etc
这是我的代码片段:
...
for (int i = 0; i < mNoOfContacts; i++) {
String stringContactUri = storeSettings.getString("contactUri"+i, "");
if (stringContactUri != ""){
Uri contactUri = Uri.parse(stringContactUri);
loadContactInfo(contactUri);
}
...
private void loadContactInfo(Uri contactUri) {
AsyncTask<Uri, Void, ContactInfo> task = new AsyncTask<Uri, Void, ContactInfo>() {
@Override
protected ContactInfo doInBackground(Uri... uris) {
return mContactAccessor.loadContact(getContentResolver(), uris[0]);
}
@Override
protected void onPostExecute(ContactInfo result) {
Contacts[mNoOfContacts] = result;
Toast.makeText(getApplicationContext(), mNoOfContacts+"Picked Contact"+Contacts[mNoOfContacts].getDisplayName(), Toast.LENGTH_SHORT).show();
mNoOfContacts++;
}
};
task.execute(contactUri);
}
...
我的代码是 Google Android 演示应用程序 - 名片的修改。 请帮忙!谢谢!
I am new to threads. I am trying to load a small array of photos. Right now I am using Async tasks/ threads, but how do I make the outcome sequential? Below is an illustration:
What I want:
a[0] = photo1;
a[1] = photo2;
a[2] = photo3;
a[3] = photo4
What my program gives me instead. Note that the order changes and is random:
a[0] = photo[2];
a[1] = photo[1];
a[2] = etc
Here's a snippet of my code:
...
for (int i = 0; i < mNoOfContacts; i++) {
String stringContactUri = storeSettings.getString("contactUri"+i, "");
if (stringContactUri != ""){
Uri contactUri = Uri.parse(stringContactUri);
loadContactInfo(contactUri);
}
...
private void loadContactInfo(Uri contactUri) {
AsyncTask<Uri, Void, ContactInfo> task = new AsyncTask<Uri, Void, ContactInfo>() {
@Override
protected ContactInfo doInBackground(Uri... uris) {
return mContactAccessor.loadContact(getContentResolver(), uris[0]);
}
@Override
protected void onPostExecute(ContactInfo result) {
Contacts[mNoOfContacts] = result;
Toast.makeText(getApplicationContext(), mNoOfContacts+"Picked Contact"+Contacts[mNoOfContacts].getDisplayName(), Toast.LENGTH_SHORT).show();
mNoOfContacts++;
}
};
task.execute(contactUri);
}
...
My code is a modification of the Google Android demo app - Business Cards.
Please help! Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的方法可能是让
onPostExecute
方法调用一个执行排序的例程:传递 ContactInfo 和 Uri,然后将 Uri 与原始 Uri 进行比较并将其放在正确的位置。或者修改您的
AsyncTask
,这样您就可以一次传递所有Uri
,然后从那里启动一个,而不是为每个 Uri 启动一个。What might be the easiest thing to do is make the
onPostExecute
method call a routine that does the sorting: pass both the ContactInfo and the Uri and then compare the Uri with the original and put it in the proper place.Alternatively modify your
AsyncTask
so instead of launching one per Uri you launch one you pass all theUri
s at once and then go from there.我认为,根据定义,“异步任务”不是同步的......异步字面意思是“不同步”。如果您希望它同步,只需通过执行 URI 调用以老式方式加载它们,获取您的照片,并在加载最后一张照片后再次执行此操作。
I would think that, by definition, "Asynchronous Tasks" are not synchronized...Asynchronous literally means "not synchronized." If you want it synchronized, just load them the old fashioned way by doing a URI call, get your photo and do it again once the last photo was loaded.