Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to synchoronize a ArrayList  RSS feed

 
Sudha
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I know Vector is a synchoronized one and ArrayList are not. How to make ArrayList synchronized?
Thanks,
Suda
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

We have a strict policy on display names, which must be a real first and last name with a space between.

Please go here and fix your display name up, pronto. Thanks, pardner!

As to your question: look at the static method java.util.Collections.synchronizedList() .
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, you guys jumped on that name thing fast. And at the same time! You should consider a future in synchronized swimming. Interpretive dance?

Sudha, see the Collections utility class in the JavaDoc. It can give you synchronized versions of various collections. Then dig into the JDK source and see if they really do what you expect.
 
Maria Smith
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sudha,

You can do this.
List list = Collections.synchronizedList(new ArrayList(...));
 
Balaji Ambresh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if you have multiple threads(i mean, not taking the same runnable parameter), then, you can synchronize the getter and setter methods at the class level.

synchronized(Myclass.class)
{// code goes here
}

If all the thread objects access this class where the ArrayList object is there, then, you can synchronize the getter and setter methods at the object level.

synchronized(this)
{// code goes here
}

hope this helps
[ March 02, 2007: Message edited by: Maximus Smith ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!