Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Java thread-safe locking mechanism for collections?

What would be the least-slow thread-safe mechanism for controlling multiple accesses to a collection in Java?

I am adding objects to the top of a collection and i am very unsure what would be the best performing collection. Would it be a vector or a queue? I originally thought an ArrayList would be fast but i ran some experiments and it was very slow.

EDIT: In my insertion testing a Vector delared using volatile seems to be the fastest?

like image 226
Jason Avatar asked Dec 07 '22 23:12

Jason


2 Answers

See collections in java.util.concurrent.

like image 121
卢声远 Shengyuan Lu Avatar answered Dec 10 '22 12:12

卢声远 Shengyuan Lu


The best algorithms avoid shared state as much as possible. It sounds like you are new to multi threaded programming in java. Why not just pick something that you know will be correct.... Then see how much synchronisation there is, then see if that is a problem...then see if you can find a faster way to do it.

like image 41
time4tea Avatar answered Dec 10 '22 11:12

time4tea