private void dwPoster() {
Log.e(TAG, "run to dwPoster() v20110919-2");
final List<Bitmap> tempBm=new ArrayList<Bitmap>();

new Thread() {
public void run() {
BestTvClient client = new BestTvClient();
List<BestXMLContents> list = client.getPosterList(); if (list != null) {
int size=list.size()>15?15:list.size();
for (int i = 0; i < size; i++) {
String url = list.get(i).getImageUrl();
Bitmap bitmap = client.parseBitmpFromUrl(url);
//Log.e(TAG, "one bigmap parse done");
if (bitmap != null) {
tempBm.add(bitmap);
}
}
if(imgTimer!=null){
// Log.e(TAG,"start cancel imgtimer...");
imgTimer.cancel();
// Log.e(TAG,"end cancel imgtimer.");
int num=imgTimer.purge();
//Log.e(TAG,"**purge num="+num);
}
mflag=false;
Log.e(TAG,"start bitmap recycle...");
for (int i = 0; i < mPosters.size(); i++) {
Bitmap bm = mPosters.get(i);
if (!bm.isRecycled()) {
bm.recycle();
bm = null;
}
}
Log.e(TAG,"end bitmap recycle");
mPosters.clear();
mCurPos = 0;
mPosters.addAll(tempBm);

Message msg = new Message();
msg.what = MSG_START_PLAY;
mHandler.sendMessage(msg);
}
}; }.start(); }
上面的代码中,要实现加粗部分的代码执行过程中不被中断(执行过程中其他线程不执行)
如何做到呢?

解决方案 »

  1.   

    mflag=false;
                        Log.e(TAG,"start bitmap recycle...");
                        for (int i = 0; i < mPosters.size(); i++) {
                            Bitmap bm = mPosters.get(i);
                            if (!bm.isRecycled()) {
                                bm.recycle();
                                bm = null;
                            }
                        }
                        Log.e(TAG,"end bitmap recycle");
                        mPosters.clear();
                        mCurPos = 0;
                        mPosters.addAll(tempBm);
                        [/b]
    就是这一段代码执行过程中不被其他线程中断
      

  2.   

    mPosters对象是怎么来的sychronized(mPosters)
      

  3.   

    mPosters是类成员变量
    另外的线程也操作这个对象
      

  4.   

    很简单,只要把要排斥的那段代码写到synchronized (this.getClass()){}里就可以了
    因为你这个线程是同一个线程类,所以单个线程实例的class是一样的