Exiting a Class Without Exiting Java

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hal Vaughan

    Exiting a Class Without Exiting Java

    I have a class that is sometimes called from the command line and sometimes
    from another class.

    When the user clicks "Quit" on the panel it opens, if it is running from the
    command line, it quits. The problem is that I need a way to terminate the
    class and the object without using quit, so it doesn't kill other classes.
    The QUIT button calls an actionListener, so if I have "return" in the
    listener, it closes the window, but the class is still active.

    How can I return from the class (does that makes sense) and get the object
    made from this class to "self destruct" without exiting the JVM?

    Thanks!

    Hal
  • Raymond DeCampo

    #2
    Re: Exiting a Class Without Exiting Java

    Hal Vaughan wrote:[color=blue]
    > I have a class that is sometimes called from the command line and sometimes
    > from another class.
    >
    > When the user clicks "Quit" on the panel it opens, if it is running from the
    > command line, it quits. The problem is that I need a way to terminate the
    > class and the object without using quit, so it doesn't kill other classes.
    > The QUIT button calls an actionListener, so if I have "return" in the
    > listener, it closes the window, but the class is still active.
    >
    > How can I return from the class (does that makes sense) and get the object
    > made from this class to "self destruct" without exiting the JVM?
    >[/color]

    It is a lot easier to discuss with concrete names, so let ServiceClass
    be the class providing the service and ClientClass be the class invoking
    ServiceClass.

    Depending on your threading needs, try the following:

    1) Register a callback object in the ServiceClass that it can invoke
    when it is done,
    2) Call notify() on an agreed upon object (perhaps even the instance of
    ServiceClass itself) when ServiceClass is finished.

    When the ServiceClass is finished, it notifies the ClientClass in one of
    the above ways. Then you can clean up ServiceClass in the appropriate
    manner. (E.g., call dispose() on Swing components, set all references
    to ServiceClass to null, etc.)

    Another potential solution is to use a modal dialog window. The thread
    invoking show() on a modal dialog window will block until the window is
    hidden. If this is appropriate for your environment, go for it.

    Ray

    Comment

    • Hal Vaughan

      #3
      Re: Exiting a Class Without Exiting Java

      Thanks for giving me a few good ideas to think about!

      Hal

      Raymond DeCampo wrote:
      [color=blue]
      > Hal Vaughan wrote:[color=green]
      >> I have a class that is sometimes called from the command line and
      >> sometimes from another class.
      >>
      >> When the user clicks "Quit" on the panel it opens, if it is running from
      >> the
      >> command line, it quits. The problem is that I need a way to terminate
      >> the class and the object without using quit, so it doesn't kill other
      >> classes. The QUIT button calls an actionListener, so if I have "return"
      >> in the listener, it closes the window, but the class is still active.
      >>
      >> How can I return from the class (does that makes sense) and get the
      >> object made from this class to "self destruct" without exiting the JVM?
      >>[/color]
      >
      > It is a lot easier to discuss with concrete names, so let ServiceClass
      > be the class providing the service and ClientClass be the class invoking
      > ServiceClass.
      >
      > Depending on your threading needs, try the following:
      >
      > 1) Register a callback object in the ServiceClass that it can invoke
      > when it is done,
      > 2) Call notify() on an agreed upon object (perhaps even the instance of
      > ServiceClass itself) when ServiceClass is finished.
      >
      > When the ServiceClass is finished, it notifies the ClientClass in one of
      > the above ways. Then you can clean up ServiceClass in the appropriate
      > manner. (E.g., call dispose() on Swing components, set all references
      > to ServiceClass to null, etc.)
      >
      > Another potential solution is to use a modal dialog window. The thread
      > invoking show() on a modal dialog window will block until the window is
      > hidden. If this is appropriate for your environment, go for it.
      >
      > Ray[/color]

      Comment

      Working...