ArrayList的 - 新增]相同的"对象(同样= GT;平等相待,哈希code),线程 [英] ArrayList - add "same" objects (same => equals, hashCode), Threads

查看:106
本文介绍了ArrayList的 - 新增]相同的"对象(同样= GT;平等相待,哈希code),线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ive得到了一个问题。当我尝试添加两次相同的对象一个ArrayList会发生什么。随着相同的我的意思是一个单独的类,它被确定为相同与equals()和hash code()的对象。它有不同的价值观对大多数成员变量和可能不同的线程创建的,但对于equals()和哈希code()的相同。
请问第二个对象,然后替换第一个对象?

Ive got one question. What happens when I try to add the "same" object twice to an ArrayList. With "the same" I mean an object of an individual class, which is identified as the same with equals() and hashCode(). It has different values for most of the member variables and was created from maybe different threads, but for equals() and hashCode() its the "same". Does the second object then replace the first object?

此外,如果两个线程试图准确地在同一时间到ArrayList添加这些对象会发生什么?这甚至可能?如果是的话,会发生什么?

Also, what happens if two threads try to add those objects exactly at the same time to the ArrayList? Is this even possible? If yes, what happens?

感谢您! :-)

感谢所有的答案!我应该使用synchronizedList然后而不是使用同步(名单){}? - >我阅读文档,甚至synchronizedList,对于同步迭代(列表)应使用

Thanks for all the answers! Should I use synchronizedList then rather then using "synchronize(list){}"? --> I read the docs, even with synchronizedList, for iterating synchronize(list) shall be used


可以在synchronizedList被声明为一个成员变量?我试过了,但它没有工作。

Can a synchronizedList be declared as a member variable? I tried, but it didnt work.

推荐答案

没有,的ArrayList 不会试图在所有检测重复 - 你可以有一个的ArrayList 与出现多次完全相同的参考。如果你想要一个集合避免重复,你需要一个设置实施 - 如果你的的要preserve插入顺序,你可能想要<一个href=\"http://download.oracle.com/javase/6/docs/api/java/util/LinkedHashSet.html\"><$c$c>LinkedHashSet.

No, ArrayList doesn't attempt to detect duplicates at all - you can have an ArrayList with the exact same reference appearing multiple times. If you want a collection to avoid duplicates, you need a Set implementation - and if you also want to preserve insertion order, you probably want LinkedHashSet.

请注意,但是,如果没有锁定的ArrayList 应的的从摆在首位多个线程突变 - 它只是并不意味着是一个以这种方式线程安全的集合。多个线程可以的阅读的从的ArrayList 不同步,但不是变异它。从文档:

Note, however, that without locking ArrayList should not be mutated from multiple threads in the first place - it's simply not meant to be a thread-safe collection in that way. Several threads can read from an ArrayList without synchronization, but not mutate it. From the docs:

请注意,此实现不是同步的。如果多个线程同时访问一个ArrayList实例,线程的至少一个结构上修改了列表,它必须保持外部同步。 (结构上的修改是指添加或删除一个或多个元素,或明确地调整大小的支持数组的操作;仅设置元素的值不是结构修改。)这一般是通过对同步某个对象自然封装的实现名单。如果不存在这样的对象名单应该是包装使用Collections.synchronizedList方法。这最好在创建时进行,以prevent意外的不同步访问列表

Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections.synchronizedList method. This is best done at creation time, to prevent accidental unsynchronized access to the list

如果您想从变异多线程的集合无锁的,我建议你看看集合在<一个href=\"http://download.oracle.com/javase/6/docs/api/java/util/concurrent/package-frame.html\"><$c$c>java.util.concurrent.

If you want to mutate a collection from multiple threads without locking, I suggest you look at the collections in java.util.concurrent.

这篇关于ArrayList的 - 新增]相同的&QUOT;对象(同样= GT;平等相待,哈希code),线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆