Да, я уверен, что этот вопрос существует, но я попробовал ответы, и, думаю, мне нужен индивидуальный ответ.
В любом случае, как следует из названия, я получаю 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;
}
}
Просто дайте мне знать, что вам еще нужно.
@EventHandler
? - person Bohemian♦   schedule 28.08.2013listeners
? - person yshavit   schedule 28.08.2013