天天看點

背景線程的調用Thead

List<ActionDevice> temDevice = new ArrayList<ActionDevice>();

ScheduleEexcuteThread SET=new ScheduleEexcuteThread(temDevice);

SET.start();

參數temDevice為需要操作的對象。

參數ActionDevice為實體類

public class ScheduleEexcuteThread extends Thread{

Logger log = Logger.getLogger(Class.class.getName());

public  List<ActionDevice>  Dealy = new ArrayList<ActionDevice>();

public  List<ActionDevice> tempDealy = new ArrayList<ActionDevice>();

Boolean stop=false; 

public  ScheduleEexcuteThread(List<ActionDevice> dDealy){

    this.Dealy=dDealy;

}

public void run(){

while(!stop){

for(ActionDevice adevice :Dealy){

//對不符合條件的對象加入臨時list中

        tempDealy.add(adevice);

   }

        Dealy.clear();

        Dealy.addAll(tempDealy);

       tempDealy.clear();

       if(Dealy.size()==0){

            this.stop=true;

     }else{

            try {

          Thread.sleep();

            } catch (InterruptedException e) {

            e.printStackTrace();

            }

      }

    }

定義參數stop,當條件成熟時,設定stop=true;等待下一次線程啟動時,會判斷stop=true,之後程序結束。

      本文轉自tianjian_0913 51CTO部落格,原文連結:http://blog.51cto.com/tianjian/1665987,如需轉載請自行聯系原作者