Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

analog setinterval in java

Tags:

java

I need to check the url (once per second)

Timer timer = new Timer();
    TimerTask task = new TimerTask() {
        public void run()
        {
            System.out.print(engine.getLocation());
        }
    };
timer.schedule( task, 1000 );

but I don't need schedule

In JavaScript it would look like

var interval = self.setInterval(function(){
    console.log(mywindow.location.href);
},1000);
like image 272
mr_nobody Avatar asked Dec 12 '22 09:12

mr_nobody


2 Answers

timer.schedule( task, 0L ,1000L);

will check every second.

http://docs.oracle.com/javase/6/docs/api/java/util/Timer.html#schedule(java.util.TimerTask,%20long,%20long)

like image 125
PeterMmm Avatar answered Dec 13 '22 22:12

PeterMmm


You can use:

timer.scheduleAtFixedRate(yourTimerTask, new Date(), 1000l);

... which will schedule execution starting now, each second.

Note that the start time and rate are not guaranteed to execute precisely time-wise.

See here for API documentation on the method.

like image 31
Mena Avatar answered Dec 13 '22 23:12

Mena