5

Possible Duplicate:
Differences between HashMap and Hashtable?

I went to an interview the other day interviewer asked me under which situation will there be a problem to use hashmap rather then hashtable? Meaning give a eg where hashtmap use will result in problem but using hashtable will resolve the problem.

He told me that the machine in which the code is run is single core!!

I gave a eg

Time        Thread1            Thread 2
   t0    tb.put("a",1)       
   t1     tb.put("a",2)          int a = tb.get("a"); 

I told that if at t1 if both t1 and t2 executes simultaniously then it will result in problem. He said that since it is a single core cpu it will never execute 2 statements in parallel

Can someone please clarify that , when will there be a problem? Any example of situation?

EDIT:I posted the question by interchaing hashmap and hashtable.I know that hashtable method are synchronized and that of hashmap are not and i had told it to him

To experient i implemted following.And the code never crashed? I dint use hashtable but still it t is a hashmap in A :)

public class MyT extends Thread {

    HashMap<String,String > a = A.t;
    @Override
    public void run() {
        while (true) {
            a.put("a", "one");
            System.out.println(Thread.currentThread().getName());
        }
    }

    public static void main(String[] args) {
        MyT t1 = new MyT();
        t1.start();
        MyT t2 = new MyT();
        t2.start();
    }
}
1
  • 4
    Mr. Pointy-Hair probably thinks that tb.put/tb.get contains only one cpu instruction :) Commented Dec 7, 2011 at 5:27

5 Answers 5

3

I think you have to do the following things first before asking:

  1. Search on stackoverflow
  2. Search on Google

The following results are obtained by above two methods:

StackOverflow: Differences between HashMap and Hashtable?

Google

what is the difference between HashMap and Hashtable

Difference between HashMap and HashTable? Can we make hashmap synchronized?

Hope that helps :)

1
  • 1
    @Jaomos:I know that methods of hashtable are synch and that of map are not.Give a me example where it will fail.Dont act too smart.Am also a java programmer and am not a new baby
    – Akshay
    Commented Dec 7, 2011 at 5:39
2

Unlike the new collection implementations, Hashtable is synchronized. That's why I could imagine a situation when using HashMap would create a problem, and using Hashtable would resolve it.

The fact that it's single core is of no consequence: if Thread1 is pre-empted in the middle of a put call, Thread2 will see an inconsistent state, and may crash.

1
  • 3
    An interview is a two-way process. If you say something right, and he says it's wrong (in a topic he chose, no less), that says something about him. If this is someone you'd be working with, it may be a warning flag.
    – yshavit
    Commented Dec 7, 2011 at 6:27
0

Well, just because it's single core, doesn't mean you can't have race conditions. It possibly (probably?) means you won't have memory visibility issues, but you can certainly run multiple threads on a single core, and they can still be scheduled such that you get race conditions.

0

Here syncoronization is the main reason why hash table should be used in this scenario. Even though a single core is used in this case, you cannot gaurantee that the tb.put("a",2) will complete its execution before tb.get("a") gets called.

This can cause inconsistancies in the output. If HashTable is used, Since it is syncronised, put is completed before get is called.

See this thread for more details

0

Here is a good link, you can refer to that: Differences between HashMap and Hashtable?

I am wondering whether they ask you exactly as you said, because it seems that he want to ask which is thread-safe and which is not.

Hashtable is thread-safe, but why he ask problem with Hashtable?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.