• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Thread Safe ArrayList  RSS feed

 
Ranch Hand
Posts: 381
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that Suppose we have to make ArrayList as a thread safe class, then, should we make all methods as synchronized or only add methods should be synchronized? I understand we have Collections.synchronizedList() method to make arraylist as thread safe but I would like to understand how we will make it on our own.
 
Marshal
Posts: 62231
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot make a List truly thread‑safe because you cannot know whether its elements are thread‑safe, but wuth that proviso, why don't you try writing a thread‑safe List class?
 
Sheriff
Posts: 23966
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First you should remind yourself of what "thread-safe" means. Next, for each method consider what has to be done to make it thread-safe. For methods which use the internal state of your ArrayList class, consider how the state could be affected by other threads while the method is using the internal state.
 
Vaibhav Gargs
Ranch Hand
Posts: 381
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we make all the methods as synchronized, then, won't it solve the purpose?
 
Campbell Ritchie
Marshal
Posts: 62231
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please go through the methods and see which don't need to be synchronised.
Please work out how you would make the stream() method thread‑safe.
 
author
Posts: 23811
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vaibhav Gargs wrote:If we make all the methods as synchronized, then, won't it solve the purpose?



Synchronized is a tool. A tool that will enable calls to be atomic. Atomic calls does not mean thread safe.

It is not a magic bullet. You have to use correctly. Multiple calls on the ArrayList, multiple calls on the elements. etc. have to be taken care of. Thread safety is not a fire and forget thing. It has to be part of your application design, etc.

Henry
 
That's my roommate. He's kinda weird, but he always pays his half of the rent. And he gave me this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!