Solved

Java Synchronized not working?

Posted on 2014-01-17
2
351 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

948 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

22 Experts available now in Live!

Get 1:1 Help Now