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();
}
}
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
No comments:
Post a Comment