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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:04:48+00:00 2026-05-26T13:04:48+00:00

How can I read an array in java in a certain time? Lets say

  • 0

How can I read an array in java in a certain time? Lets say in 1000 milliseconds.

for example:

float e[]=new float [512];
float step = 1000.0 / e.length; // I guess we need something like that?
for(int i=0; i<e.length; i++){

}
  • 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-26T13:04:49+00:00Added an answer on May 26, 2026 at 1:04 pm

    You’d need a Timer. Take a look at its methods… There’s a number of them, but they can be divided into two categories: those that schedule at a fixed delay (the schedule(... methods) and those that schedule at a fixed rate (the scheduleAtFixedRate(... methods).

    A fixed delay is what you want if you require “smoothness”. That means, the time in between executions of the task is mostly constant. This would be the sort of thing you’d require for an animation in a game, where it’s okay if one execution might lag behind a bit as long as the average delay is around your target time.

    A fixed rate is what you want if you require the task’s executions to amount to a total time. In other words, the average time over all executions must be constant. If some executions are delayed, multiple ones can then be run afterwards to “catch up”. This is different from fixed delay where a task won’t be run sooner just because one might have “missed” its cue.

    I’d reckon fixed rate is what you’re after. So you’d need to create a new Timer first. Then you’d need to call method scheduleAtFixedRate(TimerTask task, long delay, long period). That second argument can be 0 if you wish the timer to start immediately. The third argument should be the time in between task runs. In your case, if you want the total time to be 1000 milliseconds, it’d be 1000/array size. Not array size/1000 as you did.

    That leaves us with the first argument: a TimerTask. Notice that this is an abstract class, which requires only the run() method to be implemented. So you’ll need to make a subclass and implement that method. Since you’re operating over an array, you’ll need to supply that array to your implementation, via a constructor. You could then keep an index of which element was last processed and increment that each time run() is called. Basically, you’re replacing the for loop by a run() method with a counter. Obviously, you should no longer do anything if the counter has reached the last element. In that case, you can set some (boolean) flag in your TimerTask implementation that indicates the last element was processed.

    After creating your TimerTask and scheduling it on a Timer, you’ll need to wait for the TimerTask’s flag to be set, indicating it has done its work. Then you can call cancel() on the Timer to stop it. Otherwise it’s gonna keep calling useless run() methods on the task.

    Do keep the following in mind: if the work done in the run() method typically takes longer than the interval between two executions, which in your case would be around 2 milliseconds, this isn’t gonna work very well. It only makes sense to do this if the for loop would normally take less than 1 second to complete. Preferably much less.

    EDIT: oh, also won’t work well if the array size gets too close to the time limit. If you want 1000 milliseconds and you have 2000 array elements, you’ll end up passing in 0 for the period argument due to rounding. In that case you might as well run the for loop.

    EDIT 2: ah why not…

    import java.util.Random;
    import java.util.Timer;
    
    public class LoopTest {
    
        private final static long desiredTime = 1000;
    
        public static void main(String[] args) {
    
            final float[] input = new float[512];
    
            final Random rand = new Random();
            for(int i = 0; i < input.length; ++i) {
                input[i] = rand.nextFloat();
            }
    
            final Timer timer = new Timer();
    
            final LoopTask task = new LoopTask(input);
    
            double interval = ((double)desiredTime/((double)input.length));
            long period = (long)Math.ceil(interval);
    
            final long t1 = System.currentTimeMillis();
    
            timer.scheduleAtFixedRate(task, 0, period);
    
            while(!task.isDone()) {
                try {
                    Thread.sleep(50);
                } catch(final InterruptedException i) {
                    //Meh
                }
            }
    
            final long t2 = System.currentTimeMillis();
    
            timer.cancel();
    
            System.out.println("Ended up taking " + (t2 - t1) + " ms");
    
        }
    
    }
    

    import java.util.TimerTask;
    
    public class LoopTask extends TimerTask {
    
        private final float[] input;
        private int index = 0;
        private boolean done = false;
    
        public LoopTask(final float[] input) {
            this.input = input;
        }
    
        @Override
        public void run() {
    
            if(index == input.length) {
                done = true;
            } else {
                //TODO: actual processing goes here
                System.out.println("Element " + index + ": " + input[index]);
                ++index;
            }
    
        }
    
        public boolean isDone() {
            return done;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read in an array from ObjectInputStream in Java. I can
I can read settings like this, for example: final String mytest = System.getString(this.getContentResolver(), System.AIRPLANE_MODE_ON);
For what I can read, it is used to dispatch a new thread in
In Java you can read a file embedded inside a JAR-file by using the
How can I convert a String array into an int array in java? I
I want to read text file's comma separated variables in a java script array
Anyone can read the GoF book to learn what design patterns are and how
I can read the MySQL documentation and it's pretty clear. But, how does one
You can read about the 64-bit calling convention here . x64 functions are supposed
You can read this question where I ask about the best architecture for a

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.