找回密码
 FreeOZ用户注册
查看: 1040|回复: 0
打印 上一主题 下一主题

What's the Major Disadvantage of Using Synchronized Data Type as Class Members?

[复制链接]
跳转到指定楼层
1#
发表于 1-10-2009 11:27:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?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.
回复  

使用道具 举报

您需要登录后才可以回帖 登录 | FreeOZ用户注册

本版积分规则

小黑屋|手机版|Archiver|FreeOZ论坛

GMT+11, 13-12-2024 07:57 , Processed in 0.027887 second(s), 17 queries , Gzip On, Redis On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表