Abhishek Singh
Abhishek Singh

Reputation: 9765

Make an arraylist method synchronized without using Collections.synchronizedList()

What are the other ways of making an arraylist synchronized without using Collections.synchronizedList() ?

Upvotes: 1

Views: 593

Answers (4)

Kami
Kami

Reputation: 458

ConcurrentLinkedQueue is not a List but it is thread safe while accessing to its elements.

Upvotes: 0

Aivean
Aivean

Reputation: 10882

Consider using CopyOnWriteArrayList or other concurrent collections from java.util.concurrent. Also, Vector is in essence synchronized ArrayList, but this also means that it's basically the same as ArrayList wrapped with Collection.synchronizedList().

If you want us to help you with choosing appropriate collection, please specify details of your task.

Upvotes: 1

Bhupi
Bhupi

Reputation: 395

i hope there are 2 ways ,

  1. extends arraylist to your own class and override each method make it thread safe to enable synchronization

  2. user CopyOnWriteArrayList which is another thread safe version of arraylist.

Upvotes: 1

Jack
Jack

Reputation: 133639

The method Collection.synchronizedList() just wraps an existing List by synchronizing all methods that can be accessed from a client class on a single mutex.

Nothing more nor less, if you want to emulate the behavior, the simplest way is to do exactly the same. But I don't see the point in doing it.

Upvotes: 4

Related Questions