首页 > 其他 > 详细

Thread in depth 3:Synchronization

时间:2017-04-28 10:10:34      阅读:250      评论:0      收藏:0      [点我收藏+]

Synchronization means multi threads access the same resource (data, variable ,etc) should not cause a corruption to it.If all method of a class promise threading synchronization,we call that the class is "Thread Safety".

ALL static methods of the .net framework are promised thread-safe. When we are writting class and method we should pay a little bit attention on the thread safety,we can:

  1. Avoid to use static data.Different thread can access the same static data at the same time so may cause corruption to it. Use instance data or value-type data. value-type data will always be copied to the stack of the thread,so does the local variable of a instance data.
  2. Use lock construct. But this will cause a performance problem,because the thread will be blocked or being "spin-locked" and waste CPU and memory, so use it very carefully.

 

Thread in depth 3:Synchronization

原文:http://www.cnblogs.com/lwhkdash/p/6778361.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!