the field is sometimes used inside synchronized block and sometimes used without synchronization
2015-11-02 09:13
357 查看
http://stackoverflow.com/questions/28715625/is-it-safe-to-use-field-inside-and-outside-synchronized-block
Background
Our app sends emails which are queued in a database table. We've had some instances of duplicate emails being sent, so I'm implementing a lock to prevent multiple threads from sending emails simultaneously.
ReSharper is warning me that:
the field is sometimes used inside synchronized block and sometimes used without synchronization
Question
Why is ReSharper telling me this, and why might I be worried about it?
Code
Here's my (abridged) code:
解答:
Problem scenario :
We've had some instances of duplicate emails being sent, so I'm implementing a lock to prevent multiple threads from sending emails simultaneously.
So you are using
But again in the same code you use
So it's a warning to tell that your valuable resource is accessed in two different forms : one as
And it's a warning that let you know(or let you identify) issues that could arise from this contradictory usage of the resource
Additionally you might consider to re-structure the codes in such a way that your
Actually, it's the method which reads the queue and sends emails - not the repository itself - that I'm trying to prevent concurrent access to. I want threads to be able to write to the repository while one thread is reading emails and sending them. But I guess ReSharper can't tell that, so it warns me. I know that I'm actually fine with unsynchronized access to that resource, so I guess I'm safe to ignore the warning? – OutstandingBill
@OutstandingBill - From resharper : "Warnings In addition to compiler errors and warnings, ReSharper displays its own warnings that don't prevent your code from compiling but may nevertheless represent serious coding inefficiencies". Resharper is intelligence enough to say contradictory usage but you are the one know what actual usage is. If you are confirmed to use the resource well managed you could ignore the warning :) – KcDoD
@OutstandingBill Additionally you might be interested in re-tructuring the method to take parameters extracted from the field , rather than using it in mix context – KcDoD
Is it safe to use field inside and outside synchronized block?
问题:Background
Our app sends emails which are queued in a database table. We've had some instances of duplicate emails being sent, so I'm implementing a lock to prevent multiple threads from sending emails simultaneously.
ReSharper is warning me that:
the field is sometimes used inside synchronized block and sometimes used without synchronization
Question
Why is ReSharper telling me this, and why might I be worried about it?
Code
Here's my (abridged) code:
private readonly IMailQueueRepository _mailQueueRepository = new MailQueueRepository(); private static object _messageQueueLock = new object(); public void SendAllQueuedMessages(IPrincipal caller) { lock (_messageQueueLock) // Prevent concurrent callers { var message = _mailQueueRepository.GetUnsentMessage(); while (message != null) { SendQueuedMessage(message); message = _mailQueueRepository.GetUnsentMessage(); } } } public void SendQueuedMessage(IMessage message) { // I get the ReSharper warning here on _mailQueueRepository var messageAttachments = _mailQueueRepository.GetMessageAttachments(message.Id); // etc. }
解答:
Problem scenario :
We've had some instances of duplicate emails being sent, so I'm implementing a lock to prevent multiple threads from sending emails simultaneously.
So you are using
Lock()to prevent this happening, that means you need to synchronize threads accessing a common resource which in this case
_mailQueueRepository
But again in the same code you use
_mailQueueRepositorywithout a
Lock
// I get the ReSharper warning here on _mailQueueRepository var messageAttachments = _mailQueueRepository.GetMessageAttachments(message.Id); // <== Accessed without a lock
So it's a warning to tell that your valuable resource is accessed in two different forms : one as
synchronized(thread safe) and other
non-synchronized(non thread safe).
And it's a warning that let you know(or let you identify) issues that could arise from this contradictory usage of the resource
_mailQueueRepository. Choice is yours to either make all usages of
_mailQueueRepository
synchronized(use with a
lockand warning will be gone) or manage not to run for race conditions.
Additionally you might consider to re-structure the codes in such a way that your
SendQueuedMessage()is called with parameters which are extracted from
_mailQueueRepositoryavoiding mix usage.
Actually, it's the method which reads the queue and sends emails - not the repository itself - that I'm trying to prevent concurrent access to. I want threads to be able to write to the repository while one thread is reading emails and sending them. But I guess ReSharper can't tell that, so it warns me. I know that I'm actually fine with unsynchronized access to that resource, so I guess I'm safe to ignore the warning? – OutstandingBill
@OutstandingBill - From resharper : "Warnings In addition to compiler errors and warnings, ReSharper displays its own warnings that don't prevent your code from compiling but may nevertheless represent serious coding inefficiencies". Resharper is intelligence enough to say contradictory usage but you are the one know what actual usage is. If you are confirmed to use the resource well managed you could ignore the warning :) – KcDoD
@OutstandingBill Additionally you might be interested in re-tructuring the method to take parameters extracted from the field , rather than using it in mix context – KcDoD
相关文章推荐
- Android ViewTreeObserver简介
- Java 8 - 20 Examples of Date and Time API Read mo
- 清除android程序中的多余资源
- 周记(一)
- Spring整合Mybatis
- 你真的会玩SQL吗?EXISTS和IN之间的区别
- "IValueConverter" does not support converting from a string.——C# WPF 提示错误
- 参数多态、包含多态、过载多态和强制多态
- 完全解决!android studio更新 check for updates失败的问题
- Touch Handling in Cocos2D 3.x(四)
- 源码推荐(11.02):自动适配所有子view,仿苏宁易购的分类页面
- Touch Handling in Cocos2D 3.x(四)
- Touch Handling in Cocos2D 3.x(四)
- ARM指令中STM和LDM
- 2015暑期实习作品
- 用指针操作多维数组
- 编写轻量ajax组件02-AjaxPro浅析
- iOS-scrollview及其子类适配iOS7
- 编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符
- 咕咚登陆 注册 核心算法 上传步数【分析】