• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

When an ArrayList needs to synchronized ?

 
Ranch Hand
Posts: 98
MyEclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

One of my friends came up with the below question.

I have a method in which I am operating on a ArrayList like adding elements removing etc etc.
Now when n * Threads access the method will there be any issue with the ArrayList data. Should the ArrayList be Synchronized ?

It was also mentioned that ArrayList was declared and initialized in the same local method. !


Any insights will be appreciated.

Thanks !
 
Author and all-around good cowpoke
Posts: 13078
6
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It was also mentioned that ArrayList was declared and initialized in the same local method. !



Each Thread that executes that method will in fact have a separate instance of the ArrayList with the only reference stored on the calling stack.

So there is no possible conflict unless the ArrayList reference is returned or otherwise exported outside the method.

Bill

 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

aruna sydu wrote:Hi All,

One of my friends came up with the below question.

I have a method in which I am operating on a ArrayList like adding elements removing etc etc.
Now when n * Threads access the method will there be any issue with the ArrayList data. Should the ArrayList be Synchronized ?

It was also mentioned that ArrayList was declared and initialized in the same local method. !


Any insights will be appreciated.

Thanks !



No matter how many threads are running concurrently , each thread will have its own copy of local variable. now since you mentioned that ArrayList is declared and initialized in the same method ,hence it becomes local variable. so each thread will have its own copy of arraylist. so there will not be any conflicts.
 
aruna sydu
Ranch Hand
Posts: 98
MyEclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks William Brogden and gurpeet singh :-).That's helpful.


Regards,
Aruna.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic