Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding a Wait or Delay to Java

So I have some Java code that once it has ran through that part, I want it wait/delay for some amount of time. Here is the code I have:

Trajectory exampleTrajectory =
        TrajectoryGenerator.generateTrajectory(trajectory);
        TimeUnits.Second.sleep(10)

I have tried using TimeUnits.Second.sleep(10) but I keep getting an error saying that it cannot be resolved.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Instead of:

TimeUnits.Second.sleep(10)

It should be:

TimeUnit.SECONDS.sleep(10); 

Complete working example:

import java.util.concurrent.*;
class Main { 

  public static void main(String[] args) {
    System.out.println("a");

    try {
      TimeUnit.SECONDS.sleep(10); // 10 second delay
    }
    catch (InterruptedException e) {}
    
    System.out.println("b");
  }
  
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading