• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

arraylist thread safe

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I can understand the arraylist attributes like no thread safe etc

But what is thread safe, can any body please explain real time example?
what is the purpose of SynchronizedList methods while Vector available?
java.util.Concurrent class available for making the thing thread safe while why synchronizedList?
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

How can you understand "the arraylist attributes like no thread safe" if you don't understand what is thread safe? There's plenty of documentation on the subject. First of all start with the java tutorial. It also contains an example.
Vector is almost deprecated. As I read it is not recommended to use Vector instead of that use ArrayList or whenever you need a synchronized list you can create one with the mentioned Collections.synchronizedList method.
Can you tell me which part of java.util.Concurrent package are you talking about? I mean which part which can substitute the synchronized arraylist?
 
Sheriff
Posts: 22682
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

G.Sathish kumar wrote:But what is thread safe, can any body please explain real time example?


Thread safe means that two threads can change the ArrayList (or any other object) and all guarantees made by the class still hold. For instance, let's consider this simplified version of add:
The following is how two different threads should be intertwined:
Without proper synchronization, the following would be possible however:
Although the size has been increased by two, only one element was added - thread 2 overwrote the element "added" by thread 1. With synchronization you can make sure that the class is thread safe, so only the first block of code will occur.

what is the purpose of SynchronizedList methods while Vector available?


A Vector is a specific implementation. What if you get a different kind of List, like LinkedList, and you want it to be synchronized?
Also, you may want to synchronize on a List only at specific times. Vector will be synchronized even when it isn't necessary.

java.util.Concurrent class available for making the thing thread safe while why synchronizedList?


Because the java.util.concurrent package wasn't available until Java 5.0. Before that time, synchronizedList was the way to go. And again, if you get a List from somewhere (like a third party JAR) you can make it synchronized using synchronizedList.
 
Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic