Solved

Java Synchronized not working?

Posted on 2014-01-17
2
350 Views
Last Modified: 2014-01-17
So this seems like it should be pretty straightforward, but I can't for the life of me figure out what's going on.  Here's the entirety of the code that access my "arrAI" ListArray object:

	private class ArrayLock{}
	private ArrayLock lckArray = new ArrayLock();
	public void register(AI arg) {
		synchronized (lckArray) {
			arrAI.add(arg);
		}
	}
	public void unRegister(AI arg) {
		synchronized (lckArray) {
			arrAI.remove(arg);
		}
	}
	public void updateAllAI(){
		synchronized (Globals.Loader().Lock) {
			synchronized (lckArray) {
				for (AI ai : arrAI) {
					ai.update();
				}
			}
		}
	}

Open in new window


When the code runs, I get a "concurrent modification" error at the "for" line (16).  If I comment out the "remove" code, the error goes away.  arrAI exists nowhere else in the project.  I can't figure out why the sync lock doesn't seem to be working?
0
Comment
Question by:Javin007
2 Comments
 
LVL 14

Accepted Solution

by:
CPColin earned 500 total points
Comment Utility
Is arrAI an instance of ArrayList? If so, your code is failing because you're modifying the list while you're iterating over it; no other thread is interfering.

From the Javadocs:

The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException.

So, instead of using the "for-each" style of loop, you'll have to call arrAI.iterator() and use that iterator's remove() method.
0
 
LVL 4

Author Closing Comment

by:Javin007
Comment Utility
Sure enough, that was it!  Thanks!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now