When doing concurrent development it’s easy to make mistakes that may produce unexpected results. It can be very hard to track down these bugs. As a quick first look at the problem check to see how thread safety is being enforced. You should find one of the following techniques in use:
- Immutable objects can always be shared between threads without concern for thread safety. To accomplish this the object may need to be in an immutable wrapper class or marked as final/const.
- Objects that are accessed by only one thread that never changes do not need to add protective code because simultaneous access will not be possible. If a thread should die or need to be replaced the change logic will need to use of of the other another mechanisms to ensure thread safety.
- A lock must be held whenever reading or writing to a shared modifiable object. Lookout for situations of ‘check then modify’ where the code reads a value and then modifies the same variable. In these situations the lock must be held for the duration of both the read and the write.
No comments for Quick check for thread safety »
No comments yet.
Leave a comment