多线程执行顺序、速度

开启多线程之后,相当于并行执行,所以线程的run方法和原程序一样对待,并非先执行run,忽略此问题容易导致数组越界

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == UPDATE) {
//用户点击了更新按钮
//http请求必须使用线程处理
HttpDownLoad httpDownLoad = new HttpDownLoad();
httpDownLoad.start();
String xml = downloadXML("http://192.168.0.101:8080/mp3/resources.xml");
List<Mp3Info> mp3Infos = parse(xml);
//设定循环,保证线程里面的run方法先执行完成,以防httpDownLoad.list为空
//线程run执行的循环要比顺序执行SimpleAdapter显然要慢
while(httpDownLoad.list.size() < mp3Infos.size()){

}
SimpleAdapter simpleAdapter = new SimpleAdapter(this, httpDownLoad.list, R.layout.mp3info_item, new String[]{"mp3_name", "mp3_size"},new int[]{R.id.mp3_name,R.id.mp3_size});
setListAdapter(simpleAdapter);



return true;
}else if(id == ABOUT){
//用户点击了关于按钮
}
return super.onOptionsItemSelected(item);
}

private class HttpDownLoad extends Thread{
List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
public void run() {
// TODO Auto-generated method stub
String xml = downloadXML("http://192.168.0.101:8080/mp3/resources.xml");
List<Mp3Info> mp3Infos = parse(xml);
for(Iterator iterator = mp3Infos.iterator();iterator.hasNext();){
Mp3Info mp3Info = (Mp3Info)iterator.next();
HashMap<String,String> map = new HashMap<String,String>();
map.put("mp3_name", mp3Info.getMp3Name());
map.put("mp3_size", mp3Info.getMp3Size());
list.add(map);
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值