• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

How to accomplish this without threads?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}

}
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...?
 
reply
    Bookmark Topic Watch Topic
  • New Topic