马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?FreeOZ用户注册
x
For example, Vector, Hashtable, etc. Nowadays, people might use
Collections.synchronizedList(), Collections.synchronizedSet(),
and Collections.synchronizedMap() to decorate a collection data type
into synchronized one. However, using synchronized data member would
cost you a hand and a leg. The major disadvantage of using this type of
data member, I think, would be the dead-lock problem. When there are
more then two synchronized data member within a single class,
you need to be very careful on the deadlock issue.
In the other hand, please make sure you really need to apply such
a synchronize programming model in your class. Most of the time,
you will find that using outer protection on your collections
would prevent your data from dirty reading and writing very well.
It's not necessary to return a collection reference to the caller;
therefore, your collection objects would be handled within a single
class, which could dramatically reduce the scope of your objects,
so that you can handle access to them more easily.
If you really want a synchronized collection object, please make sure
you won't transfer it all over the world. Synchronizing an object doesn't
mean that making it safe. On contrast, you just make it more dangerous.
You need to refine its scope within a limited area. |