wood burning stoves 2.0*
The moose likes Java in General and the fly likes synchronizedSortedMap(new HashSortedMap()) error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "synchronizedSortedMap(new HashSortedMap()) error " Watch "synchronizedSortedMap(new HashSortedMap()) error " New topic
Author

synchronizedSortedMap(new HashSortedMap()) error

kamesh aru
Ranch Hand

Joined: Mar 16, 2002
Posts: 150
SortedMap m = Collections.synchronizedSortedMap(new HashSortedMap());
when i am trying to use above declaration fro synchronization of sorted map its giving error
symbol : class HashSortedMap
location: class timertest
SortedMap m = Collections.synchronizedSortedMap(new HashSortedMap());
^


can any one help me out
thanks in advance
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30506
    
150

Kamesh,
There is no class in the API called HashSortedMap. Perhaps you mean TreeMap? That's the only class I can think of that implements SortedMap.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
You should bookmark the Java API docs and use them to double-check the name of any class when you get such an error.

Layne


Java API Documentation
The Java Tutorial
kamesh aru
Ranch Hand

Joined: Mar 16, 2002
Posts: 150
thanks for the reply
SortedMap m = Collections.synchronizedSortedMap(new TreeMap());
now accessing m is synchronized or do i need to create this block ?

synchronized(m) { }
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Layne]: You should bookmark the Java API docs

In fact it looks like the original code came from the API for Collections - specifically, for the synchronizedSortedMap() method. But the thing to realize is that the class used in the example, HashSortedMap, was just a made-up class only for use as an example. It doesn't exist as part of the API you can use, unless you define it.

[kamesh]: now accessing m is synchronized or do i need to create this block?

Yes, each method call on m is synchronized. However there may be some cases where you need synchronization across multiple consecutive method calls; for this you may still need a synchronied(m) block. This is a somewhat complex subject, but the short version is: don't assume that just because a class is synchronized, it's synchronized correctly for what you need it to do. Often that's not the case.


"I'm not back." - Bill Harding, Twister
kamesh aru
Ranch Hand

Joined: Mar 16, 2002
Posts: 150
thanks jim for the answer
one more clarification
Yes, each method call on m is synchronized. However there may be some cases where you need synchronization across multiple consecutive method calls; for this you may still need a synchronied(m) block. This is a somewhat complex subject, but the short version is: don't assume that just because a class is synchronized, it's synchronized correctly for what you need it to do. Often that's not the case.

synchronisation needed when multiple methods call on same block ..then what is each method call on m is synchronized ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronizedSortedMap(new HashSortedMap()) error