You are not logged in.

wcf.regNote.message

Vlad

Beginner

  • "Vlad" started this thread

Posts: 12

  • Send private message

1

Friday, March 30th 2007, 5:03pm

ConcurrentModificationException

Ich muss einen kleinen Wecker programmieren auf Basis eines Frameworks unseres Professors. Soweit funktioniert alles, bis auf den Iterator, der mittels: .next(); mir eine ConcurrentModificationException wirft.
Dieses Ding bekomme ich einfach nicht weg. Ein anderes Forum gibt den Ratschlag die LinkedList, um die es sich dabei handelt einfach in eine andere zu kopieren und dort zu arbeiten, jedoch bringt das irgendwie auch nicht viel. Hier mein Code:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package net.asilaghi.pp.clock;

import java.util.ConcurrentModificationException;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.LinkedList;

import de.hhn.se.mi.clock.exceptions.InvalidParameterException;
import de.hhn.se.mi.clock.impl.ClockAdapter;
import de.hhn.se.mi.clock.impl.OneTimeAlarm;
import de.hhn.se.mi.clock.impl.TimeEventImpl;
import de.hhn.se.mi.clock.interfaces.Alarm;
import de.hhn.se.mi.clock.interfaces.AlarmListener;
import de.hhn.se.mi.clock.interfaces.SimpleAlarmClock;
import de.hhn.se.mi.clock.interfaces.TimeEvent;
import de.hhn.se.mi.clock.interfaces.TimeListener;
import de.hhn.se.mi.clock.impl.AlarmEventImpl;


public class MyClock extends ClockAdapter implements SimpleAlarmClock {

	private long updateTime;
	LinkedList<Alarm> listOfAlarms = new LinkedList();
	LinkedList<Alarm> copyAlarms = new LinkedList();
	Iterator<Alarm> it = copyAlarms.iterator();
	GregorianCalendar gc = new GregorianCalendar();
	MyTimeListener mytimelistener = new MyTimeListener();
	MyAlarmListener myalarmlistener = new MyAlarmListener();
	
	Alarm actAlarm;
	
	int sizeoflist;
	
	public MyClock(String name) {
		super(name);
		try {
			System.out.println("Neuen Alarm erstellt: Alarm1, Alarm2");
			this.addAlarm(new OneTimeAlarm(400, "Alarm1"));
			this.addAlarm(new OneTimeAlarm(600, "Alarm2"));
			System.out.println("Alarme hinzugefügt");
			copyAlarms.addAll(listOfAlarms);
			System.out.println("listOfAlarms nach copyAlarms kopiert!");
		} catch (InvalidParameterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("run(): hat wohl nicht sein sollen! :P");
		}
		this.makeATick();
	}

	@Override
	protected void makeATick() throws ConcurrentModificationException {
		// TODO Auto-generated method stub
		// schlafen
		updateTime = this.getUpdateTimeInMilliseconds();
		System.out.println("Neue Zeit: " + updateTime + " Sekunden vom 01.01.1970");
		sizeoflist = copyAlarms.size();
		System.out.println("Size of list copyAlarms: " + sizeoflist);
		System.out.println("Size of list listOfAlarms: " + listOfAlarms.size());
		
		while(it.hasNext())
		{
			System.out.println("it.hasNext() " + it.hasNext());
			actAlarm = it.next();
			
			System.out.println("Objekt aus der Liste geholt!");
						
			try {
				if(actAlarm.isAlarmTimeNow(this.getUpdateTimeInMilliseconds())) 
				{
					this.addTimeListener(mytimelistener);
					this.addAlarmListener(myalarmlistener);
					
					mytimelistener.newTime(new TimeEventImpl(this));
					myalarmlistener.newAlarm(new AlarmEventImpl(this, "Alarm"));
					
					if(this.getUpdateTimeInMilliseconds() == actAlarm.nextCheckTime())
					{
						System.out.println("Alarm1 : Rrrrring!");
					}
				}
			}
			catch (ConcurrentModificationException e)
			{
				System.out.println("FEHLER!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
			}
			
		}
	}

	public void addAlarm(Alarm alarm) throws InvalidParameterException {
		// TODO Auto-generated method stub
		listOfAlarms.add(alarm);
		System.out.println("Alarm in listOfAlarms hinzugefügt!");
	}

	public long getUpdateTimeInMilliseconds() {
		// TODO Auto-generated method stub
		return gc.getTimeInMillis();
	}

	public void removeAlarm(Alarm alarm) {
		// TODO Auto-generated method stub
		listOfAlarms.remove(alarm);
	}
}


Und dazu noch der Output:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Neuen Alarm erstellt: Alarm1, Alarm2
Exception in thread "main" java.util.ConcurrentModificationException
Alarm in listOfAlarms hinzugefügt!
Alarm in listOfAlarms hinzugefügt!
Alarme hinzugefügt
listOfAlarms nach copyAlarms kopiert!
Neue Zeit: 1175266638765 Sekunden vom 01.01.1970
Size of list copyAlarms: 2
Size of list listOfAlarms: 2
it.hasNext() true
	at java.util.LinkedList$ListItr.checkForComodification(Unknown Source)
	at java.util.LinkedList$ListItr.next(Unknown Source)
	at net.asilaghi.pp.clock.MyClock.makeATick(MyClock.java:64)
	at net.asilaghi.pp.clock.MyClock.<init>(MyClock.java:48)
	at net.asilaghi.pp.clock.Main.main(Main.java:10)


EDIT: habe es gelöst. der iterator muss nach dem befüllen der linkedList erstellt werden!
"C lets you shoot yourself in the foot rather easily. C++ allows you to reuse the bullet!"

This post has been edited 1 times, last edit by "Vlad" (Mar 30th 2007, 7:48pm)


wcf.user.socialbookmarks.titel