是的,我确定这个问题存在,但我已经尝试了答案,我想我需要一个自定义答案..
无论如何,正如标题所暗示的那样,我得到了一个java.util.ConcurrentModificationException。
所有这些都在它自己的线程中调用。
我如何举办活动:
private void setUpMainGameTimer() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
try{
TickEvent.call();
} catch (Exception e){
e.printStackTrace();
}
}
}, 0, gameSpeed);
}
我的堆栈跟踪。
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at net.njar.rpg.Event.EventCaller.call(EventCaller.java:15)
at net.njar.rpg.Event.TickEvent.call(TickEvent.java:10)
at net.njay.rpg.Main.Main$1.run(Main.java:213)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)
这是我的 EventCaller 类。
public class EventCaller<Type extends Event> {
@SuppressWarnings("unchecked")
public void call(ArrayList<Listener> listeners, Event e){
//Iterate through all of the listeners
for (Listener h : listeners){
//Iterate through each method
for (Method m : h.getClass().getMethods()){
//check if @EventHandler is present
if (m.isAnnotationPresent(EventHandler.class)){
//get params
for (Class<?> param : m.getParameterTypes()){
//check if parameter is the same type as the passed event
if (param.equals(e.getClass())) {
try {
m.invoke(h, (Type) e);
} catch(Exception e1){
e1.printStackTrace();
}
}
}
}
}
}
}
}
和滴答事件:
public class TickEvent extends Event{
private static long id = 0;
public static void call(){
id++;
TickEvent e = new TickEvent();
EventCaller<TickEvent> caller = new EventCaller<TickEvent>();
caller.call(Event.getListeners(), e);
}
public long getTickID(){
return TickEvent.id;
}
public static long getcurrentTickID(){
return id;
}
}
活动类别:
public class Event {
private static ArrayList<Listener> listeners = new ArrayList<Listener>();
public static void addListener(Listener listener){
listeners.add(listener);
}
public synchronized static ArrayList<Listener> getListeners(){
return Event.listeners;
}
}
让我知道你还需要什么。