When is CopyOnWriteArraySet useful to achieve thread

In Java , there is thread-safe version HashMap named ConcurrentHashMap and thread-safe version TreeMap named ConcurrentSkipListMap, but there is no ConcurrentHashSet for HashSet. Instead, there are usually 4 ways to use thread-safe Set : Set<String> mySet = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>()); Set<String> s = Collections.synchronizedSet(new

CopyOnWriteArraySet何时有用于实现线程

在Java ,存在名为ConcurrentHashMap的线程安全版本HashMap和名为ConcurrentSkipListMap的线程安全版本TreeMap,但没有用于HashSet的ConcurrentHashSet 。 相反,通常有4种方法可以使用线程安全的Set : Set<String> mySet = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>()); Set<String> s = Collections.synchronizedSet(new HashSet<String>()); ConcurrentSkipListSet

How the iterator is failsafe in concurrent hash map

As I know that iterator in the CopyOnWriteArrayList is thread-safe due to snapshot reference to the copy of arrayList at the time of iterator is created, and in this all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array so they do not affect the copy referred by snapshot reference and same for CopyOnWriteArraySet , But struggling in case of

迭代器如何在并发哈希映射中失效保护

据我所知, CopyOnWriteArrayList中的迭代器是线程安全的,因为在迭代器创建时由于对arrayList副本的快照引用,并且在此所有可变操作(add, set, and so on)都是通过创建一个新鲜底层数组的副本,因此它们不会影响快照引用引用的副本,对CopyOnWriteArraySet , 但是在ConcurrentHashMap情况下挣扎,所以请分享您的观点如果ConcurrentHaspMap情况下迭代器是如何失效保护的 你的问题有点含糊不清 - 你在标题中提到failsafe ,

CopyOnWriteArrayList implementation concern

According to javadoc of CopyOnWritearrayList : A thread-safe variant of ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array but I want to know why its making the fresh copy each time as it is doing this operation in the exclusive lock. Even if the list locks on mutative operations, one could still get an Iterator an

CopyOnWriteArrayList实现问题

根据CopyOnWritearrayList javadoc: ArrayList一个线程安全变体,其中所有可变操作(add,set等)都通过创建底层数组的新副本来实现 但是我想知道为什么它每次都在制作新副本,因为它是在独占锁中执行此操作。 即使列表锁定在可变操作上,仍然可以获取Iterator并循环集合,但不同步。 这些可变操作创建的新副本将不会被迭代器看到。 这允许其他线程从列表中读取,而不必担心由于列表的修改而导致的异常,如Javadocs中所

Is there an alternative to CopyOnWriteArrayList which can be sorted?

I have a collection of 'effects' I draw on an 'object' in a GUI ( gradients, textures, text etc ). The nature of the underlying system means that this effect collection can be accessed by multiple threads. The majority of operations are reads, so at the moment I'm using a CopyOnWriteArrayList which works ok. But now I need to sort the collection of effects based on their d

是否有可以排序的CopyOnWriteArrayList的替代方法?

我在GUI(梯度,纹理,文本等)中的'对象'上绘制了一系列'效果'。 底层系统的本质意味着这个效果集合可以被多个线程访问。 大部分操作都是读取操作,所以目前我正在使用可以正常工作的CopyOnWriteArrayList。 但是现在我需要根据绘制顺序对效果集合进行排序,无论何时添加新效果或更改效果的绘制顺序。 我还需要能够在forwards和reverse(iterater.next()&iterator.previous())中遍历集合。 经过一

Is ConcurrentHashMap analogy to CopyOnWriteArrayList

I use CopyOnWriteArrayList quite alot. This is especially true when Threads perform a lot of read Threads perform a little of write However, I will use Collections.synchronizedList() when Threads perform a little of read Threads perform a lot of write This is because according to CopyOnWriteArrayList Java Doc A thread-safe variant of ArrayList in which all mutative operations (add,

ConcurrentHashMap类似于CopyOnWriteArrayList

我使用CopyOnWriteArrayList相当多。 这是特别真实的时候 线程执行大量的读取 线程执行一些写操作 不过,我会在使用Collections.synchronizedList()时候 线程执行一些阅读 线程执行大量写操作 这是因为根据CopyOnWriteArrayList Java Doc ArrayList的线程安全变体,其中所有可变操作(add,set等)都通过创建底层数组的新副本来实现。 这通常太昂贵,... 当谈到ConcurrentHashMap ,我想知道在选择Concurrent

Why do I need to override the equals and hashCode methods in Java?

Recently I read through this Developer Works Document. The document is all about defining hashCode() and equals() effectively and correctly, however I am not able to figure out why we need to override these two methods. How can I take the decision to implement these methods efficiently? Joshua Bloch says on Effective Java You must override hashCode() in every class that overrides equals()

为什么我需要重写Java中的equals和hashCode方法?

最近我读了这个开发工程文件。 该文档全部关于有效且正确地定义hashCode()和equals() ,但我无法弄清楚为什么我们需要重写这两个方法。 我怎样才能做出有效实施这些方法的决定? Joshua Bloch谈到Effective Java 您必须在覆盖equals()的每个类中重写hashCode()。 如果不这样做将违反Object.hashCode()的一般合约,这会阻止您的类与所有基于散列的集合(包括HashMap,HashSet和Hashtable)一起正常运行。 让我们

Why do we need synchronized ArrayLists when we already have Vectors?

When do we use synchronized ArrayList ? We already have Vector which is synchronized. I think that you've got this wrong. ArrayList is unsynchronized, Vector is. Being synchronized means that every operation is thread safe - if you use the same vector from two threads at the same time, they can't corrupt the state. However, this makes it slower. If you are working in a single thr

为什么当我们已经有矢量时需要同步ArrayList?

我们什么时候使用同步的ArrayList ? 我们已经有了同步的Vector 。 我认为你有这个错误。 ArrayList是非同步的,Vector是。 同步意味着每个操作都是线程安全的 - 如果您同时使用来自两个线程的相同向量,则它们不会破坏状态。 但是,这会让它变慢。 如果您正在单线程环境中工作(或者列表仅限于线程并且从不共享),请使用ArrayList。 如果您正在使用共享相同集合的多个线程,请使用Vector或使用ArrayList,但以其他方

Should Java treat arrays as objects?

I have often thought it would be a good idea to allow the use of arrays as proper objects with their own methods instead of relying on helper classes like Arrays, Arrays and ArrayUtils. For example: ints.sort(); // Arrays.sort(ints); int[] onemore = ints.add(8); // int[] onemore = ArrayUtils.add(ints, 8); I am sure I am not the first with this idea but I have had trouble searc

Java应该将数组视为对象吗?

我经常认为允许将数组作为适当的对象与自己的方法一起使用,而不是依赖像数组,阵列和ArrayUtils这样的辅助类是个好主意。 例如: ints.sort(); // Arrays.sort(ints); int[] onemore = ints.add(8); // int[] onemore = ArrayUtils.add(ints, 8); 我确信我不是第一个有这个想法的人,但是我在寻找其他人之前曾经写过关于这个想法的麻烦。 任何人都可以帮助我对这个主题的一些参考? 这被认为是一个好的

Vector vs ArrayList performance

Everybody's saying that one should use vector because of the perfomance (cause Vector synchronizes after every operation and stuff). I've written a simple test: import java.util.ArrayList; import java.util.Date; import java.util.Vector; public class ComparePerformance { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>();

Vector与ArrayList的性能

每个人都说应该使用矢量因为性能(导致Vector在每次操作和东西之后同步)。 我写了一个简单的测试: import java.util.ArrayList; import java.util.Date; import java.util.Vector; public class ComparePerformance { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); Vector<Integer> vector = new Vector<Integer>();

Change from ArrayList to Vector

I'm working on an android game and I just noticed that since onTouchEvent runs on the UI thread, and the update/render methods are ran from a separate threads, both of them update an ArrayList which contains the entities. So obviously they conflict if they happen to modify the list at the same time. I read that Vector class is used exactly the same as ArrayList with the only difference tha

从ArrayList更改为Vector

我正在开发一款android游戏,我注意到由于onTouchEvent在UI线程上运行,并且更新/渲染方法从单独的线程运行,因此它们都会更新包含实体的ArrayList 。 如果他们碰巧在同一时间修改列表,显然他们会发生冲突。 我读过Vector类与ArrayList完全相同,唯一不同的是Vector是同步的,但它们不会发生冲突。 真的吗? 如果是这样,它是否有任何性能问题或我应该关注的问题? 我以前从未使用Vector类。 编辑:我的意思是从改变 A