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 7807095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:44:47+00:00 2026-06-02T02:44:47+00:00

This may sound like it’s been asked before, but it’s a bit different than

  • 0

This may sound like it’s been asked before, but it’s a bit different than the norm. I don’t want the typical (After the user has run the program, I want it to ask “would you like to go again?”). I want to start a process (by typing a certain string which I’ve accomplished) and have it run for an infinite amount of time until the user stops it. There’s no way of knowing how long it will run. This program happens to be a timer. So I need it to calculate how long it’s been running, be able to be stopped at any given moment by the user, and print out how long it ran.

As of now, I’m not using any UI/GUI. Keeping it as simple as possible.

  • 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-06-02T02:44:49+00:00Added an answer on June 2, 2026 at 2:44 am

    Set up a shutdown hook (code that runs when the JVM is halting):

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        @Override
        public void run() {
            // print stuff here
        }
    }));
    

    Get the user to type ctrlc at the command line to halt execution – your hook will run to print what you like as the JVM comes down.

    Edit:

    The above is brutal but simple, however if you didn’t want to terminate the whole JVM, you’re getting into the realm of "server events" to drive behaviour, which can take many forms to cause an action:

    • running your worker task in a separate thread and waiting for a command(s) at the terminal to halt (or other action) that thread – this is what I’d try first, and it would be very educational for you to do this
    • monitor a file system looking for the presence/absence of a file (lame, but it works with minimal code)
    • listening to ports for messages, an HTTP port of a web server is usually the weapon of choice, but we’re starting to get a bit heavier on the server side
    • monitoring a JMS queue for messages – we’re in Java EE space now with still more heaviness
    • any other "change in state" you care to implement

    Edit 2:

    This is a minimal implementation that works using a shutdown hook (start on the command line and press ctrlc to end and run the calculation code):

    public static void main(String[] args) throws Exception {
        final long start = System.currentTimeMillis();
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            @Override
            public void run() {
                double hours = (System.currentTimeMillis() - start) / 3600000d;
                System.out.println("Please enter the hourly rate");
                double hourlyRate = new Scanner(System.in).nextDouble();
                System.out.format("Program ran for %01.3f hours and cost $%02.2f", hours, hourlyRate * hours);
            }
        }));
        Thread.sleep(Long.MAX_VALUE); // Sleep "forever"
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may sound like a bit of a rhetorical question, but I ask it
This may sound like a bit of a dumb question but how do I
This may sound like a stupid question, perhaps it is. But I'm only trying
This may sound like an unusual question, but I am curious as to how
This may sound like a stupid question but I'm a beginner not just to
This may sound stupid but... When I create big SQL commands I want to
This may sound like a silly question but in D (using std.regex ) how
This may sound like a stupid question, but can DI be used everywhere where
This may sound like a simple question, but I just cannot seem to find
This may sound like a stupid question, but how can I start to use

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.