Is the class Thread Safe? If not how can I make that one Thread Safe?
Thanks in Advance,
posted 8 years ago
The class is NOT thread safe.
Reason : If two instances of the same class are created, it will create two different threads. Now, if both threads are calling this method doThings() which is synchronized, they are getting lock on their respective object and not on the "Class object". And a static variable is being shared between both the instances. To make this thread safe, a lock on the .Class instance should be acquired because only one .class instance exists.
Or You can also make doThings as a static synchronized method.