Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Hashtable synchronized? (http://www.velocityreviews.com/forums/t150053-hashtable-synchronized.html)

Knute Johnson 02-08-2006 01:09 AM

Hashtable synchronized?
 
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/

garskof 02-08-2006 02:19 AM

Re: Hashtable synchronized?
 
Yes, Hashtable is sync.

--
garskof


Thomas Fritsch 02-08-2006 02:48 AM

Re: Hashtable synchronized?
 
"Knute Johnson" <nospam@ljr-2.frazmtn.com> 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('$','@')



Thomas Hawtin 02-08-2006 03:02 AM

Re: Hashtable synchronized?
 
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/

Knute Johnson 02-08-2006 03:43 AM

Re: Hashtable synchronized?
 
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/

Knute Johnson 02-08-2006 03:44 AM

Re: Hashtable synchronized?
 
Thomas Fritsch wrote:
> "Knute Johnson" <nospam@ljr-2.frazmtn.com> 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/

snp_shailesh 04-24-2012 05:59 AM

Hashtable is said synchronized. what o/p u expect for following prog ? is it five 0,
 
Quote:

Originally Posted by Thomas Fritsch (Post 649439)
"Knute Johnson" <nospam@ljr-2.frazmtn.com> 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. :-(


All times are GMT. The time now is 12:28 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.