Java 线程环境中的Javolution FastSortedMap
我正试图找到一种替代方法,以避免在线程化环境中使用java.utils.TreeMap,因为TreeMap使用Sun JDK 1.6会消耗内存,而不会释放内存。我们有一个不断调整大小的树形图,需要按键排序:Java 线程环境中的Javolution FastSortedMap,java,multithreading,sorting,treemap,concurrenthashmap,Java,Multithreading,Sorting,Treemap,Concurrenthashmap,我正试图找到一种替代方法,以避免在线程化环境中使用java.utils.TreeMap,因为TreeMap使用Sun JDK 1.6会消耗内存,而不会释放内存。我们有一个不断调整大小的树形图,需要按键排序: public class WKey implements Comparable<Object> { private Long ms = null; private Long id = null; public WKey(Long ms, Long id
public class WKey implements Comparable<Object> {
private Long ms = null;
private Long id = null;
public WKey(Long ms, Long id) {
this.ms = ms;
this.id = id;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((ms == null) ? 0 : ms.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
WKey other = (WKey) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (ms == null) {
if (other.ms != null)
return false;
} else if (!ms.equals(other.ms))
return false;
return true;
}
@Override
public int compareTo(Object arg0) {
WKey k = (WKey) arg0;
if (this.ms < k.ms)
return -1;
else if (this.ms.equals(k.ms)) {
if (this.id < k.id)
return -1;
else if (this.id.equals(k.id)) {
return 0;
}
}
return 1;
}
}
Thread 1
-------------------------
Iterator<WKey> it = result.keySet().iterator();
if (it.hasNext()) {
WKey key = it.next();
/// Some processing here
result.remove(key);
}
Constantly retrieves the first element within the TreeMap and then
removes it.
Threads 2, 3, and 4
-------------------------
for (Object r : rs) {
Object[] row = (Object[]) r;
Long ms = ((Calendar) row[1]).getTimeInMillis();
Long id = (Long) row[0];
WKey key = new WKey(ms, id);
result.put(key, row);
}
Are bulk processing threads which process returned results from various
services, which are generally basic POJOs. POJOs are generated a key
based off their id and timestamp using the key above. I cannot
modify the POJO to implement a Comparator, so I must use this key.
After keys have been identified and process, they are inserted into a
shared tree map where they are getting pulled off in sorted order by
a processing thread.
We were using:
Map<WKey, Object[]> result =
Collections.synchronizedMap(new TreeMap<WKey, Object[]>());
We also tried using ConcurrentSkipListMap:
SortedMap<WKey, Object[]> result =
new ConcurrentSkipListMap<WKey, Object[]>();
我实例化了以下集合作为TreeMap的替换:
private FastMap<WKey, Object[]> result =
new FastSortedMap<WKey, Object[]>().shared();
私有快速映射结果=
新的FastSortedMap().shared();
但是,一旦另一个线程接触到容器。所有成员函数都开始失败。我仍然会遇到result.iterator().next(),size()返回的空值有时会挂起,result.keySet().min()非常缓慢。result.get返回null。doc中的示例都没有真正展示如何使用上面列出的并发视图。这真令人沮丧
我已经看过了,但是我担心我可能会遇到同样的问题,因为它们的许多排序集合都是从java.utils HashMaps和TreeMaps派生的。我也研究过,但它们的排序容器要求您在键和值上实现Compariable。我试图避免在“价值”上实现可比性。我不需要对这两个对象进行排序。如果我在值上实现comparable,我将只使用排序列表、队列或表。而且没有有序的地图。可能是候选人,但我必须手动同步所有内容,而且我正在努力节省时间
我已经审阅了中列出的其他项目,但是前面列出的项目似乎是我最好的选择
到目前为止,我不相信他们在他们的网站上做的所有广告。我的经验是,它们的实现非常不一致,缺少文档,并且在线程环境中执行相当缓慢。TreeMap表现出色;我只是希望它不要时不时地分配这么大的突发事件和GC。然而,我希望有人能证明我错了,甚至可以在线程环境中演示Javolutions集合的适当用法
否则,如果有人知道调整树形图大小的方法,而不使用“new”,或者已经解决了使用线程和排序映射的类似/替代实例,那么任何信息都将不胜感激 “永不释放记忆”是什么意思?GC是释放内存的唯一方式,TreeMap确实会删除对已删除项的引用。在运行时,TreeMap会占用我的大部分内存空间。使用clear()或remove()似乎无法释放此处所述的引用。这里的建议提到了重新创建树映射,然而,这是一个昂贵的操作,我正在尝试寻找替代方法。嗯?它指的是HashMap,在任何情况下,Java语言都没有释放引用的概念;您仅解除对对象的引用,GC将在您之后拾取。或者,您可能需要
ConcurrentSkipListMap
来实现正确的并发排序映射。您是正确的,应该是“s/free references/dereference/”。我尝试了ConcurrentSkipListMap,遇到了与上面提到的HashMap帖子中描述的相同的问题。我承认,我在暗示这个问题是相关的,因为它们都源自抽象地图。无论如何,我在单元测试中进一步隔离了我的问题。clear()和remove()对于TreeMap和ConcurrentSkipListMap都可以正常工作。看来我的问题可能在别处。我得再挖一些。我会看到。。。
private FastMap<WKey, Object[]> result =
new FastSortedMap<WKey, Object[]>().shared();