Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to accomplish this without threads?

 
Dhandapani Venkataraman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dhandapani Venkataraman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Avinash Pandit
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}

}
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you know that the clients finished using the instance? Is it always exactly one method call? Or several in sequence? Or ...?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic