我有下面这段代码,我希望我的线程等待,直到调用任何一个回调函数。
发出我的代码点击我正在进行synchObj
等待的行,但之后它就消失了它不会移动到任何地方。
如果在等待它之前不会如何调用notify
?
iGPlaceApi.getStreams(new Callback<mGooglePlacesApiResponse>() {
@Override
public void failure(RetrofitError retrofitError) {
String serverResponse = retrofitError.toString();
synchronized (synchObj) {
synchObj.notify();
//synchObj.notifyAll();
}
}
@Override
public void success(mGooglePlacesApiResponse googlePlacesObj, Response arg1){
nearbyPlaces = new String[googlePlacesObj.results.size()][4];
for (int i = 0; i < googlePlacesObj.results.size(); i++) {
mGooglePlaces.place place = googlePlacesObj.results.get(i);
nearbyPlaces[i][0] = place.icon;
nearbyPlaces[i][1] = place.name;
nearbyPlaces[i][2] = String.valueOf(place.geometry.location.lat);
nearbyPlaces[i][3] = String.valueOf(place.geometry.location.lng);
}
synchronized (synchObj) {
synchObj.notify();
//synchObj.notifyAll();
}
}
});
synchronized (synchObj) {
synchObj.wait();
}
Handler handler=new Handler();
Runnable thr = new Runnable() {
public void run() {
iGPlaceApi.getStreams(new Callback<mGooglePlacesApiResponse>() {
@Override
public void failure(RetrofitError retrofitError) {
String serverResponse = retrofitError.toString();
synchronized (synchObj) {
synchObj.notifyAll();
}
}
@Override
public void success(mGooglePlacesApiResponse googlePlacesObj, Response arg1) {
nearbyPlaces = new String[googlePlacesObj.results.size()][4];
for (int i = 0; i < googlePlacesObj.results.size(); i++) {
mGooglePlaces.place place = googlePlacesObj.results.get(i);
nearbyPlaces[i][0] = place.icon;
nearbyPlaces[i][1] = place.name;
nearbyPlaces[i][2] = String.valueOf(place.geometry.location.lat);
nearbyPlaces[i][3] = String.valueOf(place.geometry.location.lng);
}
synchronized (synchObj) {
synchObj.notifyAll();
}
}
});
}
};
handler.post(thr);
synchronized (synchObj) {
synchObj.wait();
}
答案 0 :(得分:1)
你不能这样做。回调将在调用getStreams
方法的同一线程上调用。
在调用方法返回之前,无法调用回调。您可能需要在另一个线程中调用getStreams
。
答案 1 :(得分:0)
你做错了。处理程序任务将在同一个线程上执行,因此在您的情况下它将永远不会被执行。如果你想等待你的任务完成,你应该像这样使用Thread:
// defined somewhere
boolean done = false;
Thread thr=new Thread(Runnable() {
public void run() {
iGPlaceApi.getStreams(new Callback<mGooglePlacesApiResponse>() {
@Override
public void failure(RetrofitError retrofitError) {
String serverResponse = retrofitError.toString();
synchronized (synchObj) {
done = true;
synchObj.notifyAll();
}
}
@Override
public void success(mGooglePlacesApiResponse googlePlacesObj, Response arg1) {
nearbyPlaces = new String[googlePlacesObj.results.size()][4];
for (int i = 0; i < googlePlacesObj.results.size(); i++) {
mGooglePlaces.place place = googlePlacesObj.results.get(i);
nearbyPlaces[i][0] = place.icon;
nearbyPlaces[i][1] = place.name;
nearbyPlaces[i][2] = String.valueOf(place.geometry.location.lat);
nearbyPlaces[i][3] = String.valueOf(place.geometry.location.lng);
}
synchronized (synchObj) {
done = true;
synchObj.notifyAll();
}
}
});
}
});
thr.start();
synchronized (synchObj) {
while (!done) {
synchObj.wait();
}
}
请注意,我不是只使用runnable,而是使用Thread,并且指示任务状态是必需的,因为等待调用可以自动唤醒,如果任务还没有完成,你必须再次调用它。