Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Hashtable synchronized?

Reply
Thread Tools

Hashtable synchronized?

 
 
Knute Johnson
Guest
Posts: n/a
 
      02-08-2006
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,

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
 
 
 
garskof
Guest
Posts: n/a
 
      02-08-2006
Yes, Hashtable is sync.

--
garskof

 
Reply With Quote
 
 
 
 
Thomas Fritsch
Guest
Posts: n/a
 
      02-08-2006
"Knute Johnson" <(E-Mail Removed)> wrote:
> 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.


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


 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      02-08-2006
Knute Johnson wrote:
> 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
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      02-08-2006
Thomas Hawtin wrote:
> Knute Johnson wrote:
>> 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


Thanks Tom.

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      02-08-2006
Thomas Fritsch wrote:
> "Knute Johnson" <(E-Mail Removed)> wrote:
>> 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.

>


Thanks Tom.

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
snp_shailesh snp_shailesh is offline
Junior Member
Join Date: Apr 2012
Posts: 1
 
      04-24-2012
Quote:
Originally Posted by Thomas Fritsch View Post
"Knute Johnson" <(E-Mail Removed)> wrote:
> 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.


--
"TFritsch$t-online:de".replace(':','.').replace('$','@')
Hi,
i am agree with the above statements. but still wondering why the output of following program is not like 5 zeros, 5 ones, and then 5 twos.
Can anyone help in understanding this ?
------------------------------------------------------------------------------

import java.util.Hashtable;
import java.util.Enumeration;

class Insects
{
Hashtable ht;
int i;

public Insects()
{
ht=new Hashtable();
ht.put("a",0);
ht.put("b",0);
ht.put("c",0);
ht.put("d",0);
ht.put("e",0);

}



public synchronized void showData()
{
try{
Enumeration e=ht.elements();
System.out.println();

while(e.hasMoreElements())
{

System.out.println((Integer)e.nextElement());
Thread.sleep(1000);

}
}
catch(InterruptedException e)
{
System.out.println(e);
}

}

public void modifyData(int i)
{
Enumeration e=ht.keys();
while(e.hasMoreElements())
{
String s=(String)e.nextElement();
ht.put(s,i);
}

}
}

class Flier extends Thread
{
Insects its;
static int i=0;
public Flier(String name,Insects it)
{
super(name);
try{
its=it;
Thread.sleep(2000);
start();
}
catch(InterruptedException e)
{
System.out.println(e);
}
}

public void run()
{
its.modifyData(i);
i++;
its.showData();

}

}

class Threader
{
public static void main(String a[])
{
Insects it=new Insects();
Flier f1=new Flier("f1",it);
Flier f2=new Flier("f2",it);
Flier f3=new Flier("f3",it);



}
}






Have created 3 threads for attacking on the hashtable. But with above program, i guess synchronization is not achieved.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: hashtable or map? (map inserts not behaving as I expect - and I cant find a decent simple example for hashtable) Kai-Uwe Bux C++ 1 12-21-2008 09:25 PM
Problem with hashTable Guillermo Perl 1 03-04-2004 12:43 PM
Type Hashtable not defined diya ASP .Net 1 10-31-2003 06:59 PM
Get Hashtable Object Directly John E ASP .Net 1 10-08-2003 12:40 PM
vbc compilation fails when using Hashtable Jonathan Wolfson ASP .Net 1 06-27-2003 04:40 PM



Advertisments