Hashtable synchronized?

K

Knute Johnson

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,
 
T

Thomas Fritsch

Knute Johnson said:
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.
 
T

Thomas Hawtin

Knute said:
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
 
K

Knute Johnson

Thomas said:
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.
 
K

Knute Johnson

Thomas said:
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.

Thanks Tom.
 
Joined
Apr 24, 2012
Messages
1
Reaction score
0
Hashtable is said synchronized. what o/p u expect for following prog ? is it five 0,

"Knute Johnson" <[email protected]> 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. :-(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top