2

I have model. This is actually an ArrayList of objects. And I also have methods of dealing with it. Such as remove(), find(), getAll(), create(), add(), loadFromFile() and storeInFile().

This model used by different clients. They both have reference to this resource. Is it fine to let them change model without synchronization? They will be able to invoke particular method at the same moment.

How do I have to make a synchronization? Do I have to synchronize methods, or collection?

Create
  • 51
  • 1
  • 1
  • 2

4 Answers4

2

It depends - if your methods do some longer logic before for example adding new object into collection, I would recommend to synchronize operations on the method level. Of course it is slower than changing ArrayList into thread-safe collection, but if you do some operations before, it allows to make sure that your data put into collection are not outdated.

Of course in the other case, synchronization of you inner collection is much better solution.

omnomnom
  • 8,911
  • 4
  • 41
  • 50
2

Is it fine to let them change model without synchronization?

NO. If it's a shared resource, clients will have inconsistent views. That is simply unacceptable.

How do I have to make a synchronization?

Use the synchronized keyword.

Do I have to synchronize methods, or collection?

I recommend you synchronize the collection, not the methods. For instance,

public void add(Object o){
    synchronized(foo){
        foo.add(o);
    }
}

where foo is your collection.


For more information, see:

mre
  • 43,520
  • 33
  • 120
  • 170
0

You need to think about each method in the model and consider the consequences of multiple threads calling methods at the same time, and what the consequences are. If any consequences are bad, use synchronization. Nobody can give you the right answer without knowing the logic behind your model.

Qwerky
  • 18,217
  • 6
  • 44
  • 80
-1

Changing from an ArrayList to a Vector should suffice. Vectors are thread-safe implementers of java.util.List. That would seem to be the easiest way to make it thread-safe, if not the fastest.

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
  • 2
    This is the quickest change, but does not explicitly indicate synchronization. Extra burden is placed on programmer: to not change the collection being used because of implicit use of synchronization. It would be better to synchronize on the level of the level of the model's api using the synchronized keyword. – Atreys Aug 09 '11 at 12:34
  • `Vector` is considered obsolete, see http://stackoverflow.com/questions/1386275/why-java-vector-class-is-considered-obsolete-or-deprecated – Qwerky Aug 09 '11 at 12:57
  • 1
    Why use the Vector class which is almost obsolete and not either synchronize access in the list's containing object or use one of the newer recommended classes from the Collection, such as an ArrayList wrapped in a Collections.synchronizedList? The latter is well spelled out in the [ArrayList API](http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html) – Hovercraft Full Of Eels Aug 09 '11 at 14:05