如何在Java中获取自定义的syncedList? [英] How to obtain a customized synchronizedList in java?

查看:95
本文介绍了如何在Java中获取自定义的syncedList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义列表MyList,它扩展了ArrayList,像这样:

I have one customized list MyList that extends ArrayList, like this:

class MyList extends ArrayList<SomeParticularItem>{
   [some methods...]
}

由于我具有对列表的并发读取和写入,因此我想对其进行同步:

Since I have concurrent reads and writes to the list, I want to synchronize it:

MyList mylist = (MyList) Collections.synchronizedList(new MyList());

这似乎很好,.jar已构建.然后,在运行时,我得到:

This seems to be fine, the .jar is build. Then, at runtime, I get:

java.util.Collections$SynchronizedRandomAccessList cannot be cast to MyList

是否有更好的方法(根本没有办法)来获取从某些java.util.List继承的列表的同步列表?

Is there a better way (is there any way at all) to obtain a synchronized list of a list that inherits from some java.util.List?

推荐答案

最简单的方法是扩展Vector而不是ArrayList.如果想将其保留为ArrayList,也可以自己同步MyList的方法.

The easiest way to do it would be to extend Vector instead of ArrayList. You could also synchronize the methods of MyList yourself if you wanted to keep it as an ArrayList.

这篇关于如何在Java中获取自定义的syncedList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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