Need help regarding writing component for timer
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Wednesday, March 28, 2001 11:30 PM

Sir I've went through your site as suggested by google search engine. I'm stuck up with one problem.I've written a java code for timer.This code will keeps on checking system time and the moment it met with the set time the code will print "Hello Nawneet !".And after 5 sec it will keep on printing the above said message till we use (ctrl+c).Here with i'm sending the code. Now problem is that i want to write component so that it can be used again and without hardcoting the set time,delay and message.And with several attempts i'm unable to do it.I need your help urgently. Looking forward for favorable reply. Yours NAWNEET ****************** my java code for timer*************** i   More>>

Sir
I've went through your site as suggested by google search engine.
I'm stuck up with one problem.I've written a java code for timer.This code will keeps on checking system time and the moment it met with the set time the code will print "Hello Nawneet !".And after 5 sec it will keep on printing the above said message till we use (ctrl+c).Here with i'm sending the code.
Now problem is that i want to write component so that it can be used again and without hardcoting the set time,delay and message.And with several attempts i'm unable to do it.I need your help urgently.
Looking forward for favorable reply.
Yours
NAWNEET


****************** my java code for timer***************

import java.util.*;

public class MyTimer extends Thread
{
int count=0;
public void run()
{
while(true)
{
try
{
Thread.sleep(5000);
System.out.println("Hello Nawneet !" +count);
count++;
}
catch(Exception e)
{}
}
}


public static void main(String args[])
{
int hour = 12;
int syshr = 0;
MyTimer mt = new MyTimer();
Date d = new Date();
System.out.println("Hello Nawneet !");
while(true)
{
syshr = d.getHours();
if(syshr==hour)
{
break;
}
}
mt.start();
}

}

   <<Less

Re: Need help regarding writing component for timer

Posted By:   Michael_Wax  
Posted On:   Sunday, April 8, 2001 02:39 PM

The following class does what you want. It takes the hour to start printing, the interval between prints, and the message as parameters, so that you can change these without recoding. For example, invoke using:

java MyTime 12 5 the message

The message is one or more parameters, to allow the use of spaces between words. The class could be made more robust, e.g., with some error checking, but that seems to go beyond what you are asking.


The code is radically refactored from yours. It no longer creates a separate print thread (no need for one), and it uses a Calendar method instead of a deprecated Date method. Actually, more refactoring is called for. Do you really want to get the time continuously to determine whether it is time to print the message, rather than sleeping for an hour at a time?



import java.util.Calendar;

public class MyTimer {

public static void main(String args[]) {
int hour = Integer.parseInt(args[0]);
int interval = Integer.parseInt(args[1]) * 1000;
StringBuffer buffer = new StringBuffer();
for (int i=2; i buffer.append(args[i]);
}
String message = buffer.toString();
Calendar calendar;
while(true) {
calendar = Calendar.getInstance();
if(Calendar.get(Calendar.HOUR_OF_DAY)==hour) {
break;
}
}

int counter = 0;
while(true) {
try {
Thread.sleep(interval);
System.out.println(message + count++);
}
catch(InterruptedException e) {}
}
}
}
About | Sitemap | Contact