hashmap和hashtable的区别|

发布于 12-19 19:28 字数 1169 浏览 2 评论 0原文

可能的重复:
HashMap 和 Hashtable 之间的区别?

有一天我去面试,面试官问我什么情况下使用hashmap而不是hashtable会出现问题? 意思是给出一个例子,其中 hashtmap 使用会导致问题,但使用 hashtable 将解决问题。

他告诉我运行代码的机器是单核的!

我给出了一个例子

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

,我告诉如果在 t1 时刻 t1 和 t2 同时执行,那么就会导致问题。 他说既然是单核cpu就永远不会并行执行2条语句

有人能澄清一下什么时候会出现问题吗? 有情况举例吗?

编辑:我通过交互 hashmap 和 hashtable 发布了这个问题。我知道 hashtable 方法是同步的,而 hashmap 方法不是同步的,我已经告诉他

为了体验我实现了以下内容。并且代码从未崩溃?我不使用哈希表,但它仍然是 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();
    }
}

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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

只为守护你2024-12-26 19:28:30

我认为你在提问之前必须先做以下事情:

  1. 在 stackoverflow 上搜索
  2. 在 Google 上搜索

通过以上两种方法得到以下结果:

StackOverflow : HashMap 和 Hashtable 的区别?

Google

HashMap 和 Hashtable 有什么区别

HashMap和HashTable的区别?我们可以使 hashmap 同步吗?

希望有帮助:)

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 :)

2024-12-26 19:28:30

与新的集合实现不同,Hashtable 是同步的。这就是为什么我可以想象使用 HashMap 会产生问题的情况,而使用 Hashtable 可以解决它。

它是单核的事实并不重要:如果 Thread1put 调用过程中被抢占,Thread2 将看到一个状态不一致,可能会崩溃。

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.

绿萝2024-12-26 19:28:30

嗯,仅仅因为它是单核,并不意味着你不能有竞争条件。这可能(可能?)意味着您不会遇到内存可见性问题,但您当然可以在单个核心上运行多个线程,并且仍然可以对它们进行调度,以便您获得竞争条件。

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.

猫七2024-12-26 19:28:30

这里同步是在这种情况下应该使用哈希表的主要原因。即使在这种情况下使用单核,您也不能保证 tb.put("a",2) 会在 tb.get("a")< 之前完成其执行。 /code> 被调用。

这可能会导致输出不一致。如果使用HashTable,由于它是同步的,所以put在调用get之前完成。

有关更多详细信息,请参阅此主题

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

羁客2024-12-26 19:28:30

这是一个很好的链接,您可以参考:HashMap 和 Hashtable 之间的区别?

我想知道他们是否完全按照你说的那样问你,因为他似乎想问哪个是线程安全的,哪个不是。

Hashtable 是线程安全的,但为什么他会问 Hashtable 的问题?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文