Syncronization: getting locks for several objects

K

Karsten Wutzke

Hi all!

Is there a nicer way than nesting synchronized blocks to get locks on
several objects at one time?

I have two threads running, which work pretty much on the same group of
objects, whenever one of them performs a step. I want to implement an
all or nothing strategy, that is, either of the two threads may start to
perform a step *anytime*, but when it starts, changes by the other
thread on these objects should be blocked. So in effect, both can start
anytime, but the one happening earlier shall block the other until its
finished.

I see no other way but to lock all objects the threads will work on up
front. But I think, there's something better - something I'm not
thinking of.

Any help is appreciated on what to do here!

Karsten
 
C

chris

Karsten said:
Hi all!

Is there a nicer way than nesting synchronized blocks to get locks on
several objects at one time?

Put that way, no, alas there isn't. However ...
I have two threads running, which work pretty much on the same group of
objects, whenever one of them performs a step. I want to implement an
all or nothing strategy, that is, either of the two threads may start to
perform a step *anytime*, but when it starts, changes by the other
thread on these objects should be blocked. So in effect, both can start
anytime, but the one happening earlier shall block the other until its
finished.

I see no other way but to lock all objects the threads will work on up
front. But I think, there's something better - something I'm not
thinking of.

Don't lock on the objects themselves, create a new object whose sole
function is to control access to the "real" objects.

Object mutex = new Object();

....

synchronized(mutex) {
change foo
change bar
...
}

Remember that locking objects in Java is a very permissive affair -
synchronising on an object doesn't prevent another thread from accessing or
changing that object, only from synchronising it. So synchronising on an
object you want to modify is just a convention - if you wanted you could
synchronise on foo every time you change bar and v.v., it would work just
as well(!).

Good luck
 
C

Chris Smith

Karsten said:
Is there a nicer way than nesting synchronized blocks to get locks on
several objects at one time?

What's wrong with the answers you got when you asked this question on
comp.lang.java.help less than 12 hours earlier?

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
K

Karsten Wutzke

Chris said:
What's wrong with the answers you got when you asked this question on
comp.lang.java.help less than 12 hours earlier?

The difference is, that I rethought a little bit of the process. I was
trying to get locks on every object used. Now, I'm beginning to think
about some other way of getting two thread to work exclusively, that is,
the one that starts earlier, finds some way of blocking the other until
finished. I've never needed something like this before, and was was
implicitly asking for a different approach to this but locking many objects.

Karsten
 

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,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top