Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6380321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:15:12+00:00 2026-05-25T02:15:12+00:00

I need to call the method a non static method removeLocksOnExit() from a ShutdownHook.

  • 0

I need to call the method a non static method removeLocksOnExit() from a ShutdownHook.
the method is declared in te same class where the main is.

In the main i have the following code:

//final Thread mainThread = Thread.currentThread();
Runtime.getRuntime().addShutdownHook(new Thread() {
    public void run() {
        // remove lock file...
        removeLocksOnExit();
    }
});

The removeLocks() cannot be decared static and therefore i canot call it directly from the new thread.

The main class contains However an action listners over the windows closing which calls the method as well. Here is an extract of the code:

addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e) {
        if (exit() == 0) {
            removeLocksOnExit();
            log.info("END");

            System.exit(0);
        }
    }
});

Perhaps i could then from the addShutdownHook fire the windows closing event. and this one will call the function fore me. ( Or create a special event for the purpose to be cached…

Is it feasible?? If yes how would you fire the event??

to have a better understanding here is the structure of my class:

public class MyTool extends JFrame{

removeLocksOnExit(){
....
...
}

 public static void main(String[] args) {
...............
.........
    //final Thread mainThread = Thread.currentThread();
    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            // remove lock file...
            removeLocksOnExit();
        }
    });
}

final MyTool inst = new MyTool(args);

MyTool(String[] args) {
        super(CustomPathModel.MyTITLE);
        setResizable(false); // FIXME find a way to auto size inner xml-editor
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                if (exit() == 0) {
                    removeLocksOnExit();
                    log.info("END");

                    System.exit(0);
                }
            }
        });

        setLookFeel();
        initAdminStatus();
        initGUI();
        addToolActionListener(this);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:15:13+00:00Added an answer on May 25, 2026 at 2:15 am

    You add your window-closing event in the constructor. Add the shutdown hook there as well.

    Beware, however, that the removeLocks method might be called twice, from different threads. Take care that it cleans up after itself, and that it is threadsafe. Also make certain that the removeLocks method is ready to run (ie, all variables that it needs have been initialized) before you create either the window listener or the shutdown hook.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a use case where I need to call a (non-static) method in
i need to call a non-static method from an async operation , for ease
i want to intercept method from a non-proxied object. I have a class instance
I have a non-static C# class with some instance methods, which I need to
I need to create a delegate to a non-static method of a class. The
I have a DLL from which I need to P/Invoke the following C method:
I need to call a method I have defined as: -(IBAction)next:(id)sender{ ... } I
I need a help from ur side. Actually I need to call a method
Instead of writing the following non-thread safe method. private static final Calendar calendar =
I need to call a wrapped function from a C++ DLL with the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.