ArrayList and synchronization, IllegalMonitorStateException

Joined
Dec 2, 2009
Messages
1
Reaction score
0
Hi ,

I have a piece of code which does the following by synchronizing on an arraylist. There are multiple threads which keep calling a method having the below code.

Also, the arraylistref reference keeps changing for each call.
i.e, in case we have 2 arraylist objects, arraylistobj1 and arraylistobj2.
Before executing the synchronized block of code, the arraylistref changes i.e it may refer to either one of the 2 arraylists (arraylistobj1 and arraylistobj2) during one call and the other one in the next call.

Synchronized(arraylistref){
arraylistref.add(data);
arraylistref.notifyAll();
}

P.S - There are multiple threads adding into the arraylist and also multiple threads doing remove frm the arraylist. Both are inside a synchronized block and doing a notifyall.

Occasionally I get a ,
java.lang.IllegalMonitorStateException
at java.lang.Object.notifyAll(Native Method)


Please help me why this exception occurs even though I have used a synchronized block of code. Also, a remedy to resolve this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top