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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:07:06+00:00 2026-06-13T12:07:06+00:00

I am trying to run a line of code every second by using System.currentTimeMillis();.

  • 0

I am trying to run a line of code every second by using System.currentTimeMillis();.

The code:

     while(true){
           long var = System.currentTimeMillis() / 1000;
           double var2 = var %2;

           if(var2 == 1.0){

               //code to run

           }//If():

        }//While

The code which I want to run, runs multiple times because var2 is set to 1.0 multiple times due to the infinite whole loop. I just want to run the code line when var2 is first set to 1.0, and then every time again when var2 becomes 1.0 after 0.0.

  • 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-13T12:07:06+00:00Added an answer on June 13, 2026 at 12:07 pm

    If you want to busy wait for the seconds to change you can use the following.

    long lastSec = 0;
    while(true){
        long sec = System.currentTimeMillis() / 1000;
        if (sec != lastSec) {
           //code to run
           lastSec = sec;
        }//If():
    }//While
    

    A more efficient approach is to sleep until the next second.

    while(true) {
        long millis = System.currentTimeMillis();
        //code to run
        Thread.sleep(1000 - millis % 1000);
    }//While
    

    An alternative is to use a ScheduledExecutorService

    ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();
    
    ses.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            // code to run
        }
    }, 0, 1, TimeUnit.SECONDS);
    
    // when finished
    ses.shutdown();
    

    The advantage of this approach is that

    • you can have a number of tasks with different periods sharing the same thread.
    • you can have non-repeating delay or asynchronous tasks.
    • you can collect the results in another thread.
    • you can shutdown the thread pool with one command.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to generate code on every build of my project using VS2012.
I am trying to run this code in android using eclips IDE. int maxrow=0;
I am trying to run a javascript line from my controller as a response
I'm trying to run the following line: Directions.loadFromWaypoints((Waypoint[])waypoints.toArray(), opts); But I'm getting: 23:41:44.595 [ERROR]
I'm trying to run my Silverlight tests via a command line (eventually MSBuild) working
I'm trying out the Runtime.exec() method to run a command line process. I wrote
Trying to run a self-hosted app on my Win 7 system, with little success.
I'm trying to write a simple batch that will loop through every line in
I'm trying to run through some html and insert some custom tags around every
I'm trying to run the in app billing sample code. I try to change

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.