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

Tasks 1
Numpty "synchronized" question with ArrayList 21
Threads and synchronization 21
Synchronization 7
Synchronization of the constructor 13
Sorting an ArrayList 0
Synchronization problem 2
Merge sort and threads 0

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top