I have an object that have to be used by only a single Thread at a time.
For example my Object contains 3 methods A, B and C and I want to lock the object (all the methods/attributes are locked) if a Thread access the method A.
The main difficultie is that I can’t modify the code of that object. I have to prevent multithreads access where i’m calling the object.
My first thought was to use the singleton pattern but i didn’t manage to make it work!
If you can’t change the code of the object, you’ll have to handle the locking outside the object. For example, you could encapsulate it in another class (maybe hiding it behind an interface), and have that wrapper class apply the synchronization: