我使用retrofit2从Web服务获取数据,但是当我从线程调用notify()时,主线程无法获取它。挂起应用程序,我不知道为什么。我希望有一个人可以帮助我 。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
synchronized (listRespones) {
while (listRespones.size() < 2) {
try {
listRespones.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
apiManager.getChannelListRespone(areaCode).enqueue(new Callback<CommonListRespone>() {
@Override
public void onResponse(Call<CommonListRespone> call, final Response<CommonListRespone> response) {
Thread channel1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (listRespones) {
CommonListRespone commonChannel = response.body();
listRespones.add(commonChannel);
listRespones.notify();
}
}});
channel1.start();
}
@Override
public void onFailure(Call<CommonListRespone> call, Throwable t) {
}});
主题1:
apiManager.getChannelListRespone(areaCode).enqueue(new Callback<CommonListRespone>() {
@Override
public void onResponse(Call<CommonListRespone> call, final Response<CommonListRespone> response) {
Thread channel2 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (listRespones) {
CommonListRespone commonChannel = response.body();
listRespones.add(commonChannel);
listRespones.notify();
}
}
});
channel2.start();
}
@Override
public void onFailure(Call<CommonListRespone> call, Throwable t) {
}
});
主题2:
Customer