Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Java Synchronized not working?

Posted on 2014-01-17
2
Medium Priority
?
388 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 2000 total points
ID: 39789879
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
ID: 39789911
Sure enough, that was it!  Thanks!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

963 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