1

This is one of my homework questions:

What are the problems using linkedlist on multithreaded program, What is the alternative?

I answered as follows and I would appreciate any other advices:

The problem is the lack of concurrency - in order to use the linked list we will have to use lock on it so that the changes made to the internal objects it holds will be relaible (since every object holds reference to the next object), once we lock the linked list we are basically 'shutting down' the options to use it with multiple threads, the alternative is an array since we can lock every item seperatly.

I'm totally not sure on my answer, any advices?...

Keppil
  • 45,603
  • 8
  • 97
  • 119
crazyPixel
  • 2,301
  • 5
  • 24
  • 48
  • Looks like a legitimate and complete answer to me. – Chris Cooper Apr 05 '13 at 11:45
  • My guess is that what you have written is what the teacher is looking for. Each element in the list holds a refrence to the next element and if several threads are adding elements to the linked list at the same time these refrences may not be syncronized – John Snow Apr 05 '13 at 11:48

2 Answers2

0

Your answer looks good.

Alternatives could be CopyOnWriteArrayList (assuming that a List suffices) if the iterations far exceed the mutations.

Ajay George
  • 11,759
  • 1
  • 40
  • 48
0

There is no thread-safe version of a LinkedList in Java SE, but you can can create a synchronized proxy of LinkedList with Collections.synchronizedList(List). There is also a lock-free thread-safe java.util.concurrent.CopyOnWriteArrayList but it is array based as can be seen from its name.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275