Synchronization question...

M

matt melton

hello
just a quick question really, wondering if anyone has had similair
experiences.

I am debugging some code that I wrote in a hurry, and I am having some
trouble synchronizing on my objects.
I have four sortedMaps created using:


SortedMap sortedMap1 = Collections.synchronizedSortedMap( new
TreeMap() );
...
SortedMap sortedMap4 = Collections.synchronizedSortedMap( new
TreeMap() );

I have two threads one that waits for things to be placed in the
sorteMap and one that places things into the map.
To save typing I wrote a method that looks like this:


public Object getFromMap( SortedMap map , Object key ){
while( true ){
synchronized( map ){
if( map.contains( key ) ){
return map.get( key );
}
else{
try{ map.wait(); } catch( InterruptedException ie ){}
}
}
}
}


if another thread posts something to a set and uses the original name
for the set

synchronized ( sortedMap1 ){
sortedMap1.put( key , object );
sortedMap1.notify();
}

will the other thread stop waiting?


I guess the question I am really asking is, does synchronization work
on the reference level or the object level. if an object has two
references and one thread is synchronized using one reference and
another synchronized using another reference, are they synchronized on
the same object.


The bug that I have is that the waiting thread never wakes up. So
either the above does not work, or I have a bug in some other part of
my code.

Thanks for any pointers or advice,

Matt Melton
 
G

Gordon Beaton

I guess the question I am really asking is, does synchronization
work on the reference level or the object level. if an object has
two references and one thread is synchronized using one reference
and another synchronized using another reference, are they
synchronized on the same object.

The monitor is part of the object itself. It doesn't matter if you
call the object "this" or "sortedMap1" or something else. As long as
they refer to the same object the synchronization will work. If that
weren't the case you'd never be able to synchronize on any reference
passed as an argument.
The bug that I have is that the waiting thread never wakes up. So
either the above does not work, or I have a bug in some other part
of my code.

That would seem to indicate that you're not waiting on the same
instance that you notify.

However I can imagine that your thread does in fact wake up, but is
unable to find the newly inserted key and goes back to waiting. Did
you implement Comparable and equals() correctly in your key class?

A couple of additional comments..

- did you mean to use map.containsKey()? If so, please don't *retype*
your code when you post it, cut and paste instead. It wastes
everyone's time trying to find bugs in code that may contain
different bugs or even no bugs at all.

- map.contains() followed by map.get() seems redundant to me.
map.get() will return the object if it was in the map, and null
otherwise. IMO there's no need for the separate check beforehand.

- you don't need to use Collections.synchronizedSortedMap(), because
the synchronization you've added on top of the basic mechanism is
sufficient, at least for the example you've shown here.

- you might as well synchronize all of getFromMap(), I see little
point in excluding just the loop test from the synchronized block
(the monitor is dropped during wait() anyway).

/gordon
 
M

matt melton

hello,


I found my bug.
You were right I wasn't placing the Object on the correct map.

Thanks for the advice.

Cheers

Matt Melton
 

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,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top