Research breakthrough possible @S-Logix pro@slogix.in

Office Address

  • 2nd Floor, #7a, High School Road, Secretariat Colony Ambattur, Chennai-600053 (Landmark: SRM School) Tamil Nadu, India
  • pro@slogix.in
  • +91- 81240 01111

Social List

How to perform an action periodically using Thread in java?

Description

The code given below demonstrates the stop watch. Code is executed for the certain duration which is given as input argument in seconds and stops execution when the mentioned time limit is reached and the time over message is displayed to user.

Sample Code
  • Filename: StopWatch.java

import javax.swing.*;
class StopWatch {
public static void main(String args[]) {
try {
String scount = JOptionPane.showInputDialog(null,"Enter duration in seconds");
int count=Integer.parseInt(scount);
for (int n=1;n<=count;n++) {
System.out.println(n);
Thread.sleep(1000);
}
JOptionPane.showMessageDialog(null,"Time Over");
}
catch (Exception e) {
}
}
}

Screenshots

Perform an action periodically using Thread in java
The code given below demonstrates the stop watch
Code is executed for the certain duration which is given as input argument in seconds and stops execution
When the mentioned time limit is reached and the time over message is displayed to user