posted 12 years ago
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.
Dhandapani
SCJP 1.4
Dhandapani
SCJP 1.4
Dhandapani Venkataraman
Greenhorn
Posts: 10
posted 12 years ago
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.
Hope this provides adequate details.
Hope this provides adequate details.
posted 12 years ago
If you want one and only one instance of ur class to be present in the system then u can use the singleton pattern.
e.g
package org.avi.singleton;
class factory{
private static factory instance = null;
private factory(){
// to prevent new factory() outside this class
}
//getInstance method is sychronized as thread which is executing this method will aquire lock on this object
public static synchronized factory getInstance(){
if(instance == null){
instance = new factory();
}
return instance;
}
}
e.g
package org.avi.singleton;
class factory{
private static factory instance = null;
private factory(){
// to prevent new factory() outside this class
}
//getInstance method is sychronized as thread which is executing this method will aquire lock on this object
public static synchronized factory getInstance(){
if(instance == null){
instance = new factory();
}
return instance;
}
}
posted 12 years ago
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
posted 12 years ago
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

See where your hand is? Not there. It's next to this tiny ad:
Thread Boost - a very different sort of advertising
https://coderanch.com/t/674455/Thread-Boost-feature
|