If you want to run your Java code every second you can make use of a while look what has a Thread.sleep function that sleeps for 1000ms i.e 1 second. Let see an example,
RunEverySecond.javapublic class RunEverySecond {
private static int x;
public static void main(String... args) throws InterruptedException {
System.out.println("This Program runs every second:");
while(true) {
x++;
System.out.println("Time elapsed: " + x +" seconds..");
Thread.sleep(1000);
}
}
}
Output:
This Program runs every second:
Time elapsed: 1 seconds..
Time elapsed: 2 seconds..
Time elapsed: 3 seconds..
Time elapsed: 4 seconds..
Time elapsed: 5 seconds..
Time elapsed: 6 seconds..
Time elapsed: 7 seconds..
Time elapsed: 8 seconds..
..

Run Java Program Every second
More Posts related to Java,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- Loading previous page using html button using JavaScript - JavaScript
- Remove ActionBar from Activity that extends appcompat-v7 - Android
- [Solution] IntelliJ: Cannot find declaration to go to, Nothing here, Java file outside of source root Errors - Java
- How to use SSH in Windows Terminal - Windows
- How to Go To /usr/local/bin on Mac Terminal? - MacOS
- Update SharePoint Online List Item using REST API, HTML, Spfx, Postman - SharePoint
- How to take screenshot on Android - Android
- Add a User as a Sudoer Using Ubuntu Linux Command Line Terminal - Ubuntu