Solved

Java Synchronized not working?

Posted on 2014-01-17
2
371 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

615 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