启动线程
List<ActionDevice> temDevice = new ArrayList<ActionDevice>();
ScheduleEexcuteThread SET=new ScheduleEexcuteThread(temDevice);
SET.start();
参数temDevice为需要操作的对象。
参数ActionDevice为实体类
线程ScheduleEexcuteThread需要继承Thread
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,之后进程结束。
ScheduleEexcuteThread SET=new ScheduleEexcuteThread(temDevice);
SET.start();
本文转自tianjian_0913 51CTO博客,原文链接:http://blog.51cto.com/tianjian/1665987,如需转载请自行联系原作者