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
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