Udemy coupon 100 off 2017 ::Udemy coupon code free :: Get 100+ Free Udemy coupons Here

Timer Using Java Thread

Timer Using Java Thread.

After 3 hours program will terminate.
You can stop program by pressing Ctrl+c.

Here is output:


Source code:


class clock implements Runnable
{
int sec=0,min=0,hrs=0;
clock()
{
new Thread(this).start();
}
public void run()
{
for( ; ; )
    {
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
}

        sec++;
          if(sec==60)
       {
       min++;
       sec=0;
       if(min==60)
       {
   hrs++;
   min=0;
   sec=0;
   if(hrs==3)
  {
                              break;
                          }
}
}
System.out.println( hrs+"  :" + min + " :" +sec);
} // for loop 
} // run 

public static void main(String args[])
{
clock c=new clock();
}
}



  • Save the file with name clock.java 
  • Complie with: javac clock.java
  • Run with: java clock


Out Put is as following :




No comments:

Post a Comment