在Java 6中同时访问列表的最佳方法 [英] Best approach to use in Java 6 for a List being accessed concurrently

查看:172
本文介绍了在Java 6中同时访问列表的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个List对象被多个线程访问。主要有一个线程,在某些情况下,两个线程,更新列表。有一到五个线程可以从此列表中读取,这取决于正在处理的用户请求的数量。
该列表不是要执行的任务的队列,它是正在被同时检索和更新的域对象的列表。

I have a List object being accessed by multiple threads. There is mostly one thread, and in some conditions two threads, that updates the list. There are one to five threads that can read from this list, depending on the number of user requests being processed. The list is not a queue of tasks to perform, it is a list of domain objects that are being retrieved and updated concurrently.

现在有几种方法以使对此列表的访问线程安全:

- 使用同步块

- 使用正常锁定(即读取和写入操作共享同一锁定)

-use ReadWriteLock

- 使用其中一个新的 ConcurrentBLABLBA 集合类

Now there are several ways to make the access to this list thread-safe:
-use synchronized block
-use normal Lock (i.e. read and write ops share same lock)
-use ReadWriteLock
-use one of the new ConcurrentBLABLBA collection classes

我的问题:

使用最佳方法是什么,因为cricital节通常不包含大量操作(大多数只是添加/删除/

My question:
What is the optimal approach to use, given that the cricital sections typically do not contain a lot of operations (mostly just adding/removing/inserting or getting elements from the list)?
Can you recommend another approach, not listed above?

某些约束您可以推荐另一种方法,

- 最佳性能至关重要,内存使用不那么重要

-it必须是有序列表(当前在一个 ArrayList 上同步),虽然不是排序列表(即不使用Comparable或Comparator排序,但根据插入顺序)

- 列表将很大,包含多达100000个域对象,因此使用类似CopyOnWriteArrayList的操作不可行

- 写/更新ciritical部分通常很快,做简单的添加/删除/插入或替换(设置​​)

- 读操作将主要做一个elementAt(index)调用大多数时间,虽然一些读操作可能做二进制搜索,或indexOf(元素)

- 没有直接迭代完成,虽然像indexOf(..)的操作将遍历列表

Some constrains
-optimal performance is critical, memory usage not so much
-it must be an ordered list (currently synchronizing on an ArrayList), although not a sorted list (i.e. not sorted using Comparable or Comparator, but according to insertion order)
-the list will is big, containing up to 100000 domain objects, thus using something like CopyOnWriteArrayList not feasible
-the write/update ciritical sections are typically very quick, doing simple add/remove/insert or replace (set)
-the read operations will do primarily a elementAt(index) call most of the time, although some read operations might do a binary search, or indexOf(element)
-no direct iteration over the list is done, though operation like indexOf(..) will traverse list

推荐答案

你必须使用顺序列表吗?如果映射类型结构更合适,可以使用 ConcurrentHashMap 。使用列表, ReadWriteLock 可能是最有效的方法。

Do you have to use a sequential list? If a map-type structure is more appropriate, you can use a ConcurrentHashMap. With a list, a ReadWriteLock is probably the most effective way.

编辑以反映OP的编辑:二进制搜索插入顺序?你在二进制搜索中存储时间戳并将其用于比较吗?如果是,您可以使用时间戳作为键, ConcurrentSkipListMap 作为容器(它维护键顺序)。

Edit to reflect OP's edit: Binary search on insertion order? Do you store a timestamp and use that for comparison, in your binary search? If so, you may be able to use the timestamp as the key, and ConcurrentSkipListMap as the container (which maintains key order).

这篇关于在Java 6中同时访问列表的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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