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

  • Home
  • SEARCH
  • 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 8831137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:14:42+00:00 2026-06-14T08:14:42+00:00

I need to create an object that will stop execution for a certain amount

  • 0

I need to create an object that will stop execution for a certain amount of time using its own class methods. How do I have a program keep track of passing time and execute a function when a specified amount of time has passed.

I imagine …….

long pause; //a variable storing pause length in milliseconds.............
long currentTime; // which store the time of execution of the pause ,............. 

and when another variable tracking time has the same value as currentTime + pause, the next line of code is executed. Is it possible to make a variable that for a short period changes every millisecond as time passes?

  • 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-14T08:14:43+00:00Added an answer on June 14, 2026 at 8:14 am

    For a simple solution, you could just use Thread#sleep

    public void waitForExecution(long pause) throws InterruptedException { 
        // Perform some actions...
        Thread.sleep(pause);
        // Perform next set of actions
    }
    

    With a timer…

    public class TimerTest {
    
        public static void main(String[] args) {
            Timer timer = new Timer("Happy", false);
            timer.schedule(new TimerTask() {
    
                @Override
                public void run() {
                    System.out.println("Hello, I'm from the future!");
                }
            }, 5000);
    
            System.out.println("Hello, I'm from the present");
        }
    }
    

    And with a loop

    long startAt = System.currentTimeMillis();
    long pause = 5000;
    System.out.println(DateFormat.getTimeInstance().format(new Date()));
    while ((startAt + pause) > System.currentTimeMillis()) {
        // Waiting...
    }
    System.out.println(DateFormat.getTimeInstance().format(new Date()));
    

    Note, this is more expensive then the other two solutions as the loop continues to consume CPU cycles, where as the Thread#sleep and Timer use a internal scheduling mechanism that allows the threads to idle (and not consume cycles)

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

Sidebar

Related Questions

I need to create a class that will store a unique object elements. I
In my project I need to create a business object validation layer that will
I need to create a global object that will work and can be used
I need some kind of cross-process synchronisation object that will allow me to create
I have a need to create a library of Object Oriented PHP code that
I need to create interface MultiLingual, that allows to display object's data in different
i need to create a object Image from import javax.microedition.lcdui.Image; using a file .png
I need to create a new Hash object using two arrays. But, the conditions
I am using the liveTwitter plugin The problem is that I need to stop
I need to create an instance of an object and the type of that

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.