This week's book giveaways are in the Refactoring and Agile forums. We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line! See this thread and this one for details.
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.