synchronizing on multiple objects

K

Karl Gorden

If I have two objects and I want to ensure nothing happens on both of them
while I do an operation, is it ok to synchronize twice, aka

synchronized (obj1) {

synchronized (obj2) {

// work here


}
}


or

sychronized public void method(Object obj) {

sychronized (obj) {




}
}
 
M

Matt Humphrey

Karl Gorden said:
If I have two objects and I want to ensure nothing happens on both of them
while I do an operation, is it ok to synchronize twice, aka

synchronized (obj1) {

synchronized (obj2) {

// work here


}
}


or

sychronized public void method(Object obj) {

sychronized (obj) {




}
}

It's ok to need to enter two monitors as part of a more sophisticated
scheme, but it is very important that you work out all the possibilities by
which the two locks could be acquired. In particular, you must ensure that
it is impossible (truly impossible) for another thread to hold the second
lock and wait for the first as this will lead to deadlock. Arbitrarily
adding more locks just makes it easier to deadlock without actually
preventing errors.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
D

Dobromir Gaydarov

Karl Gorden said:
If I have two objects and I want to ensure nothing happens on both of them
while I do an operation, is it ok to synchronize twice, aka

Yes, it is.
synchronized (obj1) {

synchronized (obj2) {

// work here


}
}


You have to be carefull, since similar constructs can easily lead to a
deadlock.
It is as simple as having another execution path that synchronizes on the
same 2 object in the oposite order of obtaining the locks.
The eventually you will get into the situation when one of them obtains
obj1, the other one obtains obj2 and then both wait for the other object -
indefinetely.

or

sychronized public void method(Object obj) {

sychronized (obj) {




}
}

This will work too.

My personal preference is for the second one, although I can not really
explain why.

Regards,
Dobromir
 
L

Lee Fesperman

Karl said:
If I have two objects and I want to ensure nothing happens on both of them
while I do an operation, is it ok to synchronize twice, aka

synchronized (obj1) {

synchronized (obj2) {

// work here

}
}

or

sychronized public void method(Object obj) {

sychronized (obj) {

}
}

Either is fine. When you do a nested synchronize on mulitple objects, you have to worry
about deadlocks, as other posters mentioned.

There are several ways to avoid deadlock, but the easiest to apply is to always
synchronize the same two objects in the same order.
 
C

Chris Uppal

Lee said:
There are several ways to avoid deadlock, but the easiest to apply is to
always synchronize the same two objects in the same order.

Or to arrange that access to both objects is synchronised on a single, shared,
"lock" object.

-- chris
 

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

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top