1

Hi i want to know is there any performance issue if i make ArrayList thread safe using Collections.synchronizedList() method? if there is issue, on which functionality it affect like addition ,searching etc.

I am getting concurrent Modification Exception to get rid of it i used Collections Synchronized method but later i came to know it has some performance issue? my question is very simple which approach should i use without affecting performance...

2
  • 1
    Why don't you simply use Vector? Commented Nov 20, 2013 at 9:41
  • Its a valid question.. Downvoters please give reasons.. Commented Nov 20, 2013 at 9:46

5 Answers 5

3

all methods of synchronized list use synchronization which will work slower than direct accesss.

java.util.Vector is an implementation of List which already has all methods synchronized

Sign up to request clarification or add additional context in comments.

1 Comment

java.util.Vector is almost deprecated. Avoid using Vector and Hashtable in your code. Use other collections and use Collections.synchronizedList() and similar methods if and only if you really need synchronization.
0

Synchronization itself almost does not affect performance since java 1.4. However applications that are designed with a lot of synchronization blocks can run slowly because different threads are waiting each other.

There are other approaches that help us to design multi threaded applications without or with minimal synchronization.

  1. use stateless architecture
  2. use immutable objects
  3. think about actors model

2 Comments

are you sure synchronization will not affect performance?because my array List is heavy . It is having more than 100 records and i performs various operation on it...thanks for your reply @AlexR .
@saurabh More than 100 elements in a List is ridiculously small. You can consider it to be a big list if there's 100,000 elements in it, and even then the performance issues aren't with synchronization itself.
0

yeah, you incur the overhead of threads being blocked...

If you read your list more than you write to it, java.util.concurrent.CopyOnWriteArrayList is an option for you. Reads will be fast with no locking.

Comments

0

You can use this approach as you are getting immutable list instance so no worry for multiple threads acccessing it. List immutablelist = Collections.unmodifiableList(list);

Comments

0

The problem with Collections.synchronizedList((List<T> list)) is that: in many cases, it is not really useful. For exmple, for iterating over all elements, or when getting a value, do computations based on it, and replace it with the result, we have to use manual synchronization anyway.

Fromt the documentation of Collections.synchronizedList((List<T> list))

It is imperative that the user manually synchronize on the returned list when iterating over it:

  List list = Collections.synchronizedList(new ArrayList());
      ...
  synchronized (list) {
      Iterator i = list.iterator(); // Must be in synchronized block
      while (i.hasNext())
          foo(i.next());
  }

Failure to follow this advice may result in non-deterministic behavior.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.