Hashtable synchronized?

Discussion in 'Java' started by Knute Johnson, Feb 8, 2006.

  1. 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/
     
    Knute Johnson, Feb 8, 2006
    #1
    1. Advertising

  2. Knute Johnson

    garskof Guest

    Yes, Hashtable is sync.

    --
    garskof
     
    garskof, Feb 8, 2006
    #2
    1. Advertising

  3. "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?

    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 Fritsch, Feb 8, 2006
    #3
  4. 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/
     
    Thomas Hawtin, Feb 8, 2006
    #4
  5. 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, Feb 8, 2006
    #5
  6. Thomas Fritsch 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?

    > 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/
     
    Knute Johnson, Feb 8, 2006
    #6
  7. Knute Johnson

    snp_shailesh

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

    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. :-(
     
    snp_shailesh, Apr 24, 2012
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry
    Replies:
    4
    Views:
    131,916
    tonni
    Aug 11, 2010
  2. Pep
    Replies:
    6
    Views:
    29,322
  3. dmcreyno
    Replies:
    9
    Views:
    9,590
    Mark Space
    Jun 27, 2006
  4. Knute Johnson

    Re: synchronized HashMap vs. HashTable

    Knute Johnson, May 21, 2008, in forum: Java
    Replies:
    9
    Views:
    3,264
    javabudy
    Jan 5, 2011
  5. ankur
    Replies:
    4
    Views:
    1,466
    Eric Sosman
    Nov 28, 2008
Loading...

Share This Page