Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java Synchronized not working?

Posted on 2014-01-17
2
Medium Priority
?
383 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 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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

721 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