Thursday, October 16, 2008

When Hashtable is better than Dictionary

We all know Hashtable class - it exists in .NET from the very beginning. .NET 2.0 has brought a generic replacement to it: Dictionary. So we usually prefer using it rather than Hashtable. But it isn't well known Hashtable has a very nice feature related to concurrent usage, which Dictionary does not have (at least by documentation):

- Hashtable supports single writer and multiple readers concurrent access policy
- But Dictionary - standard single writer or multiple readers concurrent access policy

This means usage of Hashtable is perfect e.g. in concurrent caching scenarios: only write operations on it require locking to ensure thread safety, but in case with Dictionary both read and write operations require locking.

The official "approval" of this fact is here.

And a fly in the ointment:
- It's difficult to find out if this was fully correct for .NET 2.0, since this fact was mentioned only recently. The issue report related to this is here.
- This is not completely true: here is the report.

Possible workarounds:
- Never clear or remove items from Hashtable at all. In many cases this is ok, e.g. when you cache something for a lifetime of the cache or the whole application. An example is caching of Reflection.Emit results or any other code loaded into AppDomain: you anyway can't unload the code, so there is no reason to clean up any references to it from caches.
- Implement item removal by dropping the old Hashtable and copying its content (without the item to remove) to a new one. Or implement a wrapper doing the same using two Hashtables (one contains just added keys and values, and andother one just removed keys) and periodically doing the garbage collection be re-creating both of them as described above. But actually the cost of looking into two Hashtables must be close to the cost of locking.

In any case, this feature is very nice even with all the mentioned problems. We utilize this feature of Hashtable e.g. in our ThreadSafeDictionary class.

29 comments:

  1. Replies
    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.
      or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

      Delete
  2. I found your this post while searching for some related information on blog search...Its a good post..keep posting and update the information.
    Download From Warez

    ReplyDelete
  3. This is a great web site. Good sparkling user interface and very informative blogs. I will be coming back in a bit, thanks for the great article. I have found it enormously useful. downlodable driver

    ReplyDelete
  4. If you have a lot of work that needs currency conversion, multiple currencies together with excel, this is a great tool. downloadfreethingssite.ru Peter has a reputation for &'ratting&' his collegues out in times of trouble, so if you&'re gonna break policy, don&'t let Peckerhead see you.

    ReplyDelete
  5. You can now access Magic Pencil using your Facebook or Google+ Email account. link for you Little Monster at School Wanderful interactive storybook in UK English and French for iPhone.

    ReplyDelete
  6. When her poetry failed togain recognition, she produced her first and best known novel, TheLeavenworth Case (1878). medicals-news.ru Added the top independent coffeehouses in ParisUpdated and optimized for iOS 8, iPhone 6 and iPhone 6 PlusNumerous bug fixes.

    ReplyDelete
  7. Never miss an activity because you dont have a ride. downlodable pdf We are a leader in education innovationcreating award-winning toys and reading and mobile learning systems that make play and learning even more fun.

    ReplyDelete
  8. - Quick reference math tables for counting, addition, subtraction, and multiplication. This page. Do you want to check out the weather for your city quickly without having to enter zip codes or navigating a complicated application?

    ReplyDelete
  9. From iJour: Studying the life of aristocratic families in Tibetan history is a rather difficult topic for Tibetologists. link for you 6) The Feed is meant to be customized to your liking.

    ReplyDelete
  10. Most actions have a template you can use to configure how the data will look when it's passed forward. concerning hobbits piano sheet music pdf Auto sync option to auto sync every newly created business card to cloud.

    ReplyDelete
  11. We've provided info/help buttons for each sub-step. http://p-like.ru Die App sortiert auf Wunsch auch nach Schwierigkeit.

    ReplyDelete
  12. NOTE: You must use two fingers on the screen to define the "box" in which the shape is drawn. salemetr.ru ATTENTION USERS THAT LOST THEIR DATA IN THE LAST UPDATE.

    ReplyDelete
  13. Position eligibility indicators that can be customized to your league's games played by position requirements. http://awesomedownloadfiles.pro If the Sun can ride roller coasters, anything is possible.

    ReplyDelete
  14. I love that there are thoroughly thought through lessons to break writing into chunks for students. http://awesomedownloadfilesfast.us - Use the scroll bar to scrub a thumbnail view of the page.

    ReplyDelete
  15. This list of the 100 most frequently used Mongolian words is compact enough to fit a little review into you busy schedule. downloadmacgames.me The problem with most German course is that it is not strictly focused on teaching spoken language.

    ReplyDelete
  16. Interactive shopping lists with name brand options. link for you Our 3D tour fuses faith with cutting age 3D technology making it possible to bring the sites to you.

    ReplyDelete
  17. PROGRESSIVE Progressive mode grows increasingly harder as you play. downloadsoftwarefromviolet.online DefragMentor Premium can defragment files on a one-time basis, while your computer is idle, during system bootup, or on a recurring schedule.

    ReplyDelete
  18. It does not change with dictionary/language and diacritics are ignored. http://awesomedownloadfilesfast.us Compared with the cost of a one-week subscription, subscribing for one month offers a 25% discount and for one year, a 35% discount.

    ReplyDelete
  19. More screen area for web browser and easier navigation. bestfromaria.us Tap the list icon while chatting on a server to send customizable commands or messages.

    ReplyDelete