Java how to write a timer
I wanted to write a timer in java.which will do the following: when program starts,start the timer1 which will stop after 45 mins, at the same time start the second timer, which will stop after 15 mins. at this time the first timer will starts again, and repeat the above loop until the program exits first timer : 45 min (the time I can use computer) second timer: 15 min (the pause time) first timer : 45 min (the time I can use computer) second timer: 15 min (the pause time) first timer : 45 min (the time I can use computer) second timer: 15 min (the pause time)
I dont know how to use the thread and timer (utils,swing) so I tried to use while(true) but the cpu goes up. here is my current code
static int getMinute(){
Calendar cal=Calendar.getInstance();
int minute=cal.getTime().getMinutes();
return minute;
}
public static Runnable clockf(){
if (endTime>=60){
endTim开发者_StackOverflowe=endTime-60;}
System.out.println(startTime);
System.out.println(currentTime);
System.out.println(endTime);
if(currentTime==endTime){
pauseStart=getMinute();
currentTime=getMinute();
pauseEnd=pauseStart+15;
if(currentTime==pauseEnd){
pauseStart=0;
pauseEnd=0;
startTime=getMinute();
currentTime=getMinute();
endTime=startTime+45;
}
}
else{
update();
}
return null;
}
private static void update() {
currentTime=getMinute();
System.out.println(currentTime);
}
public static void main(String[] args) {
startTime=getMinute();
currentTime=getMinute();
endTime=startTime+45;
Thread t=new Thread(clockf());
t.setDaemon(true);
t.start();
try {
Thread.currentThread().sleep(1000);//60000
} catch (InterruptedException e) {
System.err.println(e);
}
}
but it isnt good. are there any way to make the clockf method run only once / min ? or any other way to make that timer runs ?
Even though I did not fully understand what you're trying to do Timer and TimerTask should do that for you. Following code has to improved a bit to be runnable, but hopefully shows the principle:
long minute = 1000*60;
Timer timer1 = new Timer();
long delay1 = 45*minute;
Timer timer2 = new Timer();
long delay2 = 15*minute;
TimerTask tt1;
TimerTask tt2;
...
tt1 = new TimerTask()
{
public void run()
{
//do something and:
timer2.schedule(tt2, delay2);
}
};
tt2 = new TimerTask()
{
public void run()
{
//do something and:
timer1.schedule(tt1, delay1);
}
};
timer1.schedule(tt1, delay1);
The fastest code to write and easiest to maintain is something that you don't write at all.
I'd look into a timer and job scheduler like Quartz to see if it could help you.
There are some major problems in your code and your understanding of the Thread-classes. What I assume you are trying to do, is to define a Runnable that you pass to the thread. What you actually do, however, is execute the clockf() function as a paramter to the constructor of Thread.
If you do need a timer, look at the Java-Timer class: http://java.sun.com/j2se/1.4.2/docs/api/java/util/Timer.html
If you need to write it yourself, study the Thread class and especially the semantics of sleep and wait.
I solve the problem with Timer Now I can use the computer 45 min, then pause 15 min Many thank for all of your help, and special for kai1968 ^^ and this site http://www.roseindia.net/java/example/java/util/CertainAndRepeatTime.shtml
and can anyone tell me what static means? I dont really know why static should be there.
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Timer;
import java.util.TimerTask;
public class clock2 {
static long minute = 1000*1;//60;
static Timer timer1 = new Timer();
static long delay1 = 60*minute;
static Timer timer2 = new Timer();
static long delay2 = 45*minute;
static TimerTask tt1;
static TimerTask tt2;
static String s;
static String getSecond(){
Calendar calendar = new GregorianCalendar();
int second = calendar.get(Calendar.SECOND);
s=Integer.toString(second);
return s;
}
public static void timer(){
tt1=new TimerTask(){
public void run(){
getSecond();
System.out.println(s+"Begin");
}
};
tt2=new TimerTask(){
public void run(){
getSecond();
System.err.println(s+"Stop");
}
};
timer1.schedule(tt1 ,0,delay1);
timer2.schedule(tt2 ,delay2,delay1);
}
public static void main(String[] args) {
timer();
}
}
精彩评论