日期:2014-05-20 浏览次数:20661 次
public class Manager {
//还有很多属性
protected Manager(){
//初始化属性
Schedule.instance.addManager(this);
}
public void changeStatus() {
//具体业务
}
}
//单例
public class Schedule {
private CopyOnWriteArrayList<Manager> managerList = new CopyOnWriteArrayList<Manager>();
private ScheduledThreadPoolExecutor stpe = new ScheduledThreadPoolExecutor(1);
public static final Schedule instance = new Schedule();
private Schedule(){
stpe.scheduleWithFixedDelay(new Monitor_Runnable(), 1, 1, TimeUnit.SECONDS);
}
//将manager加入到单例的Scheudle中
public void addManager(Manager manager){
this.managerList.add(manager);
}
//监听manager
class Monitor_Runnable implements Runnable{
@Override
public void run() {
for (Manager manager : managerList) {
//manager.的一些状态操作
manager.changeStatus();
}
}
}
}