public void monitorSoldier(MyMessage myMsg) { /** 开启多线程 */ Thread[] threadList = new Thread[Values.THREAD_NUMBER]; for (int i = 0; i < Values.THREAD_NUMBER; i++) { Soldier soldier = new Soldier(); soldier.myMsg = Utils.deepCopyMsg(myMsg); soldier.threadId = i + 1; Thread soldierThread = new Thread(soldier); soldierThread.start(); threadList[i] = soldierThread; } /** 轮询查看下载进度,并告知UI线程 */ boolean monitorLive = true; long t1 = System.currentTimeMillis(); Log.d("johnchain", "Commander commanderLive = " + Values.commanderLive); while (!Thread.currentThread().isInterrupted() && monitorLive && Values.commanderLive) { try { Thread.sleep(2000); } catch (InterruptedException e) { Log.e("johnchain", "Commander get interrupt signal from UI thread"); break; } if (Values.downloadedBlock >= Values.blockNum) { Log.i("johnchain", "Commander downloaded already finished"); monitorLive = false; } Message msg = commanderToMainHandler.obtainMessage(); msg.what = Values.MT_PROG; Bundle bundle = new Bundle(); try { int prog = (Values.downloadedBlock * 100 / Values.blockNum); bundle.putInt("TotalProg", prog); bundle.putString("filename", Values.fileName); msg.setData(bundle); // Log.d("johnchain" ,"Commander prog = " + prog + " [" + Values.downloadedBlock + // "/" + Values.blockNum + "]"); Values.timeCost = (System.currentTimeMillis() - t1) / 1000; commanderToMainHandler.sendMessage(msg); } catch (ArithmeticException e) { Log.e("johnchain", "Commander Uninitialed blockNum, 0"); monitorLive = false; } } Values.soldierLive = false; for (Thread thread : threadList) { if (thread.isAlive()) { try { thread.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }