Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Hashtable synchronized?

Thread view: 
Knute Johnson - 08 Feb 2006 01:09 GMT
The docs say that Hashtable is synchronized.  Does that mean that I
don't have to prevent concurrent gets/puts with my own synchronization?
 I'm not concerned about manipulating the Iterators just storing and
retrieving elements.

Thanks,

Signature

Knute Johnson
email s/nospam/knute/

garskof - 08 Feb 2006 02:19 GMT
Yes, Hashtable is sync.

--
garskof
Thomas Hawtin - 08 Feb 2006 02:45 GMT
> The docs say that Hashtable is synchronized.  Does that mean that I
> don't have to prevent concurrent gets/puts with my own synchronization?
>  I'm not concerned about manipulating the Iterators just storing and
> retrieving elements.

Each get and put will be correctly synchronised.

However, sequences of operations will not be. Consider:

    Value value = table.get(key);
    if (value == null) {
        value = new Value();
        table.put(key, value);
    }

Between the get and put another thread may have inserted an entry under
the same key. So either synchronise the whole block against the
Hashtable, or from 1.5 use a ConcurrentMap and replace put with putIfAbsent.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Knute Johnson - 08 Feb 2006 03:43 GMT
>> The docs say that Hashtable is synchronized.  Does that mean that I
>> don't have to prevent concurrent gets/puts with my own
[quoted text clipped - 17 lines]
>
> Tom Hawtin

Thanks Tom.

Signature

Knute Johnson
email s/nospam/knute/

Thomas Fritsch - 08 Feb 2006 02:48 GMT
> The docs say that Hashtable is synchronized.  Does that mean that I don't
> have to prevent concurrent gets/puts with my own synchronization?
Yes, exactly.
Looking into the source of Hashtable, you see that all its data-accessing
methods (get, put, isEmpty, size, clear, remove, ......) are synchronized by
themselves. So there is no need for you to guard them with your own sync.

>  I'm not concerned about manipulating the Iterators just storing and
> retrieving elements.

Signature

"TFritsch$t-online:de".replace(':','.').replace('$','@')

Knute Johnson - 08 Feb 2006 03:44 GMT
>> The docs say that Hashtable is synchronized.  Does that mean that I don't
>> have to prevent concurrent gets/puts with my own synchronization?
[quoted text clipped - 5 lines]
>>  I'm not concerned about manipulating the Iterators just storing and
>> retrieving elements.

Thanks Tom.

Signature

Knute Johnson
email s/nospam/knute/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.