• 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
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Thread Safe ArrayList  RSS feed

 
Ranch Hand
Posts: 364
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: 60806
190
  • 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: 23714
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: 364
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: 60806
190
  • 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
Sheriff
Posts: 23603
138
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!