package innerclasses.controller;public abstract class Event {
private long eventTime;
protected final long delayTime;
public Event(long delayTime){
this.delayTime=delayTime;
start();
}
public void start(){
eventTime=System.nanoTime()+delayTime;
}
public boolean ready(){
return System.nanoTime()>=eventTime;
}
public abstract void action();
}package innerclasses.controller;
import java.util.*;
public class Controller {
private List<Event> eventList=new LinkedList<Event>();
public void addEvent(Event c){eventList.add(c);}

/*public void run(){
Iterator<Event> it=eventList.iterator();
List<Event> delList=new LinkedList<Event>();
while(eventList.size()>0){
it=eventList.iterator();
while(it.hasNext()){
Event e=it.next();
if(e.ready()){
System.out.println(e);
e.action();
//delList.add(e);
}

}
}

}*/
/*public void run(){
Iterator<Event> it=eventList.iterator();
//Event e=it.next();
//e.action();
while(eventList.size()>0){
it=eventList.iterator();
while(it.hasNext()){
Event e=it.next();
if(e.ready()){
System.out.print(e);
//e.action();
}
}
}
}*/
public void run(){
Iterator<Event> it=eventList.iterator();
List<Event> delList=new LinkedList<Event>();
while(eventList.size()>0){
it=eventList.iterator();
/*for(Event e:new ArrayList<Event>(eventList))
if(e.ready()){
System.out.println(e);
e.action();
eventList.remove(e);
}*/
while(it.hasNext())//尼玛找不出来是什么问题啊
{
Event e=it.next();//迭代器的next能否直接给对象赋值
if(e.ready()){

System.out.println(e);
e.action(); //action注释后运行无碍
//eventList.remove(e);
delList.add(e);
}
}
eventList.removeAll(delList);
}

}
}package com.leo.lesson10.Test19;
import innerclasses.controller.*;
public class GreenhouseControls extends Controller{
private boolean isblow=false;
public class BlowerOn extends Event{
public BlowerOn(long delayTime){super(delayTime);}
public void action(){
isblow=true;
}
public String toString(){return "Blower is on";}
}
public class BlowerOff extends Event{
public BlowerOff(long delayTime){super(delayTime);}
public void action(){
isblow=false;
}
public String toString(){return "Blower is off";}
}
private boolean light=false;
public class LightOn extends Event {
public LightOn(long delayTime){ super(delayTime);}
public void action(){
light=true;
}
public String toString() {return "Light is on";}
}
public class LightOff extends Event {
public LightOff(long delayTime){super(delayTime);}
public void action(){
light=false;
}
public String toString(){return "Light is off";}
}
private boolean water=false;
public class WaterOn extends Event {
public WaterOn(long delayTime){super(delayTime);}
public void action(){
water=true;
}
public String toString(){
return "Greenhouse water is on";
}
}
public class WaterOff extends Event{
public WaterOff(long delayTime){super(delayTime);}
public void action(){
water=false;
}
public String toString(){
return "Greenhouse water is off";
}
}
private String thermostat="Day";
public class ThermostatNight extends Event{
public ThermostatNight(long delayTime){
super(delayTime);
}
public void action(){
thermostat="Night";
}
public String toString(){
return "Thermostat on night setting";
}
}
public class ThermostatDay extends Event{
public ThermostatDay(long delayTime){
super(delayTime);
}
public void action(){
thermostat="Day";
}
public String toString(){
return "Thermostat on day setting";
}
}
public class Bell extends Event{
public Bell(long delayTime){super(delayTime);}
public void action(){
addEvent(new Bell(delayTime));
}
public String toString(){return "Bing!";}
}
public class Restart extends Event {
private Event[] eventList;
public Restart(long delayTime,Event[] eventList){
super(delayTime);
this.eventList=eventList;
for(Event e:eventList)
addEvent(e);
}
public void action(){
for(Event e:eventList){
e.start();
addEvent(e);
}
start();
}
public String toString(){
return "Restarting system";
}
}
public static class Terminate extends Event{
public Terminate(long delayTime){super(delayTime);}
public void action(){System.exit(0);}
public String toString(){return "Terminating";}
}
}
class GreenhouseControlsNew extends GreenhouseControls{
private boolean isSpray=false;
public class SprayMachineOn extends Event{
public SprayMachineOn(long delayTime){
super(delayTime);
}
public void action(){
isSpray=true;
}
public String toString(){
return "Spray machine is on";
}
}
public class SprayMachineOff extends Event{
public SprayMachineOff(long delayTime){
super(delayTime);
}
public void action(){
isSpray=true;
}
public String toString(){
return "Spray machine is off";
}
}
}package com.leo.lesson10.Test19;
import innerclasses.controller.*;
public class GreenhouseController {

public static void main(String[] args) {
GreenhouseControls gc=new GreenhouseControls();
gc.addEvent(gc.new Bell(900));
Event[] eventList={
gc.new ThermostatNight(0),
gc.new LightOn(200),
gc.new LightOff(400),
gc.new WaterOn(600),
gc.new WaterOff(800),
gc.new ThermostatDay(1400),
gc.new BlowerOn(1600),
gc.new BlowerOff(1800)
};
gc.addEvent(gc.new Restart(2000,eventList));
//gc.new Restart(0,eventList);
if(args.length==1)
gc.addEvent(new GreenhouseControls.Terminate(new Integer(args[0])));
gc.run();
}
}这是《Thinking In Java》 第十一章的练习13我做后出现的异常情况不知是什么原因造成的也不清楚该如何解决。iterator

解决方案 »

  1.   

    异常如下
    Bing!
    Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.LinkedList$ListItr.checkForComodification(Unknown Source)
    at java.util.LinkedList$ListItr.next(Unknown Source)
    at innerclasses.controller.Controller.run(Controller.java:52)
    at com.leo.lesson10.Test19.GreenhouseController.main(GreenhouseController.java:22)
      

  2.   


    while(it.hasNext())//尼玛找不出来是什么问题啊
    {
    Event e=it.next();//迭代器的next能否直接给对象赋值
    if(e.ready()){

    System.out.println(e);
    e.action(); //action注释后运行无碍
    //eventList.remove(e);
    delList.add(e);
    }
    }
    eventList.removeAll(delList);
    }

    }
    }集合类在遍历的时候是不能对其做改变的,e.action(); //action注释后运行无碍
    //eventList.remove(e);
    不知道你的e.action做了什么,下面eventList.remove(e)就改变了正在被遍历的eventList
      

  3.   

    他的代码在循环list的时候,调e.action();时候又往list里加东西,这样不错才怪了!!!
      

  4.   

    兄台,我是在另一个list里加东西而不是那个遍历的list。
      

  5.   


    你自己去debug下吧,老大
      

  6.   

    你 action 调了addEvent,你看看你自己的addEvent怎么写的:public void addEvent(Event c) {
    eventList.add(c);
    }