I have a scenario where there is a class and importantly it cannot implement Runnable or extend Thread. I have a static factory method to return the instance of the class.I have to ensure that not more than one external application uses my class's instance at any particular point of time. Initially, I thought of having a isAlreadyActive() method returning a boolean so that the external applications can check this and proceed with acquiring the instance.But now I would like to make this automated in my class itself as there is possibility that the externa apps may miss to call isAlreadyActive() method.
If you want details, you would have to tell us how, precisely, these external applications are accessing your object. Via a socket? RMI? Servlets? A Web service? The answer strongly depends on which of these we're talking about.
Sorry. I will try to put things more clear atleast this time. The class that I want to design is in a framework. The external applications are the ones that use this framework object.The external applications acquires the instance of my class thru a manager class which is there to provide instance creation services. They are all in the same layer so no network/RMI/Socket/WebSetvice communication is required.
How are the multiple applications running so that they have the potential to access the critical object at the same time? About all I can imagine (this early in the day) is that they are on separate threads inside the same JVM. If that's the case, can you use the Singleton as shown above and synchronize all the methods?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
How do you know that the clients finished using the instance? Is it always exactly one method call? Or several in sequence? Or ...?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus