This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronizedSortedMap(new HashSortedMap()) error

 
kamesh aru
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34422
347
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
kamesh aru
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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.
 
kamesh aru
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic