This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • 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
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Thread Safe ArrayList  RSS feed

 
Ranch Hand
Posts: 391
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: 63843
209
  • 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: 24295
55
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: 391
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: 63843
209
  • 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: 23824
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
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!