#5, First Floor, 4th Street , Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 pro@slogix.in

Office Address

  • #5, First Floor, 4th Street Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 Landmark : Samiyar Madam
  • 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