Java Timer klasa
U Javi, Timer je klasa koja pripada java.util paket. Proširuje Objekt klase i implementira Serializable sučelje. Klasa pruža konstruktore i metode koje se mogu koristiti za izvođenje aktivnosti povezanih s vremenom. Pomoću klase Timer možemo zakazati zadatak koji želimo izvršiti u određeno vrijeme.
Imajte na umu da je klasa Timer klasa sigurna za niti. To znači da samo nit može izvršiti metodu timer klase. Još jedna točka koju treba primijetiti je da klasa koristi strukturu podataka binarne gomile za pohranu svog zadatka.
Timer Java programi
Planiranje zadatka koji treba izvršiti nizom vremena
ScheduleTimer.java
import java.util.Calendar; import java.util.Timer; import java.util.TimerTask; public class ScheduleTimer { public static void main(String args[]) { //instance of the Timer class Timer timer = new Timer(); TimerTask task = new TimerTask() { //represent the time after which the task will begin to execute int i = 5; @Override public void run() { if(i>0) { System.out.println(i); i--; } else { System.out.println('Wish You Very Happy Birthday!!'); //cancel the task once it is completed timer.cancel(); } } }; //creating an instance of the Calendar class Calendar date = Calendar.getInstance(); //setting the date and time on which timer will begin date.set(2022, Calendar.MARCH, 30,23, 59, 54); //enables the counter to count at a rate of 1 second timer.scheduleAtFixedRate(task, date.getTime(), 1000); } }
Izlaz:
prilagođena iznimka u Javi
5 4 3 2 1 Wish You Very Happy Birthday!!
Pogledajmo još jedan Java program u kojem smo izvršili zadatak nakon određenog vremenskog intervala.
TimerDemo.java
import java.util.Timer; import java.util.TimerTask; public class TimerDemo { Timer timer = new Timer(); TimerDemo(int seconds) { //schedule the task timer.schedule(new RemindTask(), seconds*1000); } class RemindTask extends TimerTask { public void run() { System.out.println('You have a notification!'); //terminate the timer thread timer.cancel(); } } //driver code public static void main(String args[]) { //function calling new TimerDemo(10); } }
Izlaz:
You have a notification!
Programu je potrebno 10 sekundi da prikaže poruku na konzoli.
Pokretanje i zaustavljanje mjerača vremena
StartStopTimer.java
import java.util.Timer; import java.util.TimerTask; class Task extends TimerTask { int counter; public Task() { counter = 0; } public void run() { counter++; System.out.println('Ring ' + counter); } public int getCount() { return counter; } } public class StartStopTimer { private boolean running; private Task task; private Timer timer; public StartStopTimer() { timer = new Timer(true); } public boolean isRinging() { return running; } public void startRinging() { running = true; task = new Task(); timer.scheduleAtFixedRate(task, 0, 3000); } public void doIt() { running = false; System.out.println(task.getCount() + ' times'); task.cancel(); } public static void main(String args[]) { StartStopTimer phone = new StartStopTimer(); phone.startRinging(); try { System.out.println('started running...'); Thread.sleep(20000); } catch (InterruptedException e) { } phone.doIt(); } }
Izlaz:
Slično tome, također možemo stvoriti mjerač vremena pomoću klase Timer.