线程同步问题
============问题描述============
我想让下面的mythread中的循环执行完毕之后再在主线程里给messageList设置Adapter。
Thread myThread = new Thread(new Runnable() { @Override public void run() { try { Map<String, String> map1 = new HashMap<String, String>(); map1.put("", ""); String retStr = ""; retStr = NetTool.sendGetRequest(messageShowUrl, map1, "utf-8"); String[] str = retStr.split(","); list = Arrays.asList(str); listAll.clear(); for (int i = 0; i < str.length / 5; i++) { Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("user_name", list.get(i * 5)); map2.put("object_words", list.get(i * 5 + 1)); map2.put("my_words", list.get(i * 5 + 2)); map2.put("date", list.get(i * 5 + 4)); // 如果用户没有传照片,map2中传的imageUrl就为空 System.out.println("list3 = " + list.get(i * 5 + 3)); if (!list.get(i * 5 + 3).equals(" ")) { String imageUrl1 = "http://10.0.2.2:8080/BBStuServlet/" + list.get(i * 5 + 3); String imageUrl = imageUrl1.replace(" ", ""); map2.put("imageUrl", imageUrl); } else { map2.put("imageUrl", ""); } listAll.add(map2); } } catch (Exception e) { e.printStackTrace(); } } }); myThread.start(); MyAdapter adapter = new MyAdapter(this); messageList.setAdapter(adapter);
于是我改成了下面的形式:
Thread myThread = new Thread(new Runnable() { @Override public void run() { try { Map<String, String> map1 = new HashMap<String, String>(); map1.put("", ""); String retStr = ""; retStr = NetTool.sendGetRequest(messageShowUrl, map1, "utf-8"); String[] str = retStr.split(","); list = Arrays.asList(str); listAll.clear(); synchronized (this) { for (int i = 0; i < str.length / 5; i++) { Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("user_name", list.get(i * 5)); map2.put("object_words", list.get(i * 5 + 1)); map2.put("my_words", list.get(i * 5 + 2)); map2.put("date", list.get(i * 5 + 4)); // 如果用户没有传照片,map2中传的imageUrl就为空 System.out.println("list3 = " + list.get(i * 5 + 3)); if (!list.get(i * 5 + 3).equals(" ")) { String imageUrl1 = "http://10.0.2.2:8080/BBStuServlet/" + list.get(i * 5 + 3); String imageUrl = imageUrl1.replace(" ", ""); map2.put("imageUrl", imageUrl); } else { map2.put("imageUrl", ""); } listAll.add(map2); } notifyAll(); } } catch (Exception e) { e.printStackTrace(); } } }); myThread.start(); synchronized (myThread) { try { myThread.wait(); MyAdapter adapter = new MyAdapter(this); messageList.setAdapter(adapter); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
但是运行的时候直接就黑屏没有响应了,请问这是怎么回事?到底是哪里不对
============解决方案1============
你这里 myThread.wait();了,是在主线程里wait了啊,在等着线程里的notifyAll呢,当然会黑掉
你应该用handler来发消息
============解决方案2============
楼主,1楼的说法是正确的,这里我把你的代码整理了下 ,红色字体为关键处
package app.example.testapp;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
Thread myThread = new Thread(new Runnable() {
@Override
public void run() {
try {
Map<String, String> map1 = new HashMap<String, String>();
map1.put("", "");
String retStr = "";
retStr = NetTool.sendGetRequest(messageShowUrl, map1,
"utf-8");
String[] str = retStr.split(",");
list = Arrays.asList(str);
listAll.clear();
for (int i = 0; i < str.length / 5; i++) {
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("user_name", list.get(i * 5));
map2.put("object_words", list.get(i * 5 + 1));
map2.put("my_words", list.get(i * 5 + 2));
map2.put("date", list.get(i * 5 + 4));
// 如果用户没有传照片,map2中传的imageUrl就为空
System.out.println("list3 = " + list.get(i * 5 + 3));
if (!list.get(i * 5 + 3).equals(" ")) {
String imageUrl1 = "http://10.0.2.2:8080/BBStuServlet/"
+ list.get(i * 5 + 3);
String imageUrl = imageUrl1.replace(" ", "");
map2.put("imageUrl", imageUrl);
} else {
map2.put("imageUrl", "");
}
listAll.add(map2);
}
handler.sendEmptyMessage(0);//给Handler发送消息
} catch (Exception e) {
e.printStackTrace();
}
}
});
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what==0) {//接收到对应的消息进行处理
MyAdapter adapter = new MyAdapter(this);
messageList.setAdapter(adapter);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myThread.start();// 开启线程
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。