File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to accomplish this without threads? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to accomplish this without threads?" Watch "How to accomplish this without threads?" New topic
Author

How to accomplish this without threads?

Dhandapani Venkataraman
Greenhorn

Joined: Nov 30, 2003
Posts: 10
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Dhandapani Venkataraman
Greenhorn

Joined: Nov 30, 2003
Posts: 10
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

Joined: Oct 27, 2003
Posts: 5
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

Joined: Jan 29, 2003
Posts: 8791
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
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to accomplish this without threads?
 
Similar Threads
how make method complition in eclipse
Doubt on Thread Lock
Question about external system in class and component diagram
LDAP authentication in a web application.
run applet from java class