1

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());

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

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

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?

ptikobj
  • 2,690
  • 7
  • 39
  • 64
  • In my humble opinion it's usually better to not extend Java.util lists classes. Why do you need this extension ? – Manuel Selva Jul 06 '11 at 15:20
  • Are there other options if I want to have a class that is like a synchronized List, but has should have additional methods? – ptikobj Jul 19 '11 at 14:46

2 Answers2

2

Well why not make MyList synchronized, alternativly simply use the List interface List mylist = Collections.synchronizedList(new MyList());

Edit: You could of course let MyList extend Vector, since all of the Vectors methods are already synchronized you save some work.

Leonard Brünings
  • 12,408
  • 1
  • 46
  • 66
  • That's what I'm doing above, I'm only casting List to MyList because I need a MyList instance, since all my other methods want a MyList. – ptikobj Jul 07 '11 at 06:53
  • @ptikobj: Now you know why it's better to program against the `List` interface. If your `MyList` adds additional features, I'd concur with Damokles and suggest synchronizing yourself. – musiKk Jul 07 '11 at 06:59
  • But in this case, how can I add custom methods to MyList? The problem is that the "mylist" reference from your code example is a List and not a MyList. Because of this, the static code checks of Eclipse tell me that I cant invoke "customMethod1" on MyList because it is a List and "customMethod1" is unknown for objects of type List. How can I solve this? – ptikobj Jul 19 '11 at 14:36
1

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.

OpenSauce
  • 8,533
  • 1
  • 24
  • 29
  • as it turns that that Vector shouldn't be used anymore, this is not an option for me. – ptikobj Jul 19 '11 at 14:26
  • @ptikobj: as you wish. That means you'll have to synchronize the class yourself. However, I'm not sure what you mean by "Vector shouldn't be used anymore", the class isn't deprecated and it's still a perfectly good synchronized list. – OpenSauce Jul 19 '11 at 14:34
  • [this](http://stackoverflow.com/questions/1386275/why-java-vector-class-is-considered-obsolete-or-deprecated) discussion says that vector is considered obsolete or deprecated. – ptikobj Jul 19 '11 at 14:38
  • @ptikobj: it does list some problems with it... but those also apply to any answer to your question - if you use a list which is always synchronized, either because you've written it that way or via a call to `Collections.synchronizedList`, then you get the performance overhead mentioned by Jon Skeet. So in this case I still don't see why you shouldn't use `Vector` - the other option (possibly the "best" option) is to leave the class unsynchronized and only synchronize on instances of it when you need to. – OpenSauce Jul 19 '11 at 14:56
  • So for a "fully"-synchronized ArrayList, it shouldn't make any difference if I use vector or Collections.synchronizedList(new ArrayList()), right? I just wondered because in the discussion I linked above, everyone seems so happy about the new Collections.synchronizedList, but in this case, I can't see any benefit of using it. Thanks for your answers. – ptikobj Jul 19 '11 at 15:00
  • Glancing at the source code for Collections and for Vector, it looks like it wouldn't make a difference. Some advantages of Collections.synchronizedList would be: 1) only synchronize when you want, instead of synchronizing all instances; 2) separation of concerns as mentioned in other thread (can also synchronize other List implementations); 3) don't have to put up with the peculiarities of `Vector`, e.g. `set` and `setElementAt` methods which do the same thing – OpenSauce Jul 19 '11 at 15:42