"why is this class not thread safe?" Code Answer

3

since the increment method is static it will synchronize on the class object for the threadsafeclass. the decrement method is not static and will synchronize on the instance used to call it. i.e., they will synchronize on different objects and thus two different threads can execute the methods at the same time. since the ++ and -- operations are not atomic the class is not thread safe.

also, since count is static, modifying it from decrement which is a synchronized instance method is unsafe since it can be called on different instances and modify count concurrently that way.

By guya on August 14 2022

Answers related to “why is this class not thread safe?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.