更新時間:2022-09-28 15:42:01 來源:動力節(jié)點 瀏覽2569次
在Java中,有幾種情況對時間敏感的任務(wù),比如倒計時定時器被添加到Java形式。在這些計時器的時間,直到一個函數(shù)可以設(shè)置由用戶觸發(fā)。它將持續(xù)運行的情況下必須連續(xù)觸發(fā)的函數(shù)。當達到計數(shù)停機時,可以重置計時器。內(nèi)置的Java包可用于設(shè)置的時間和定期執(zhí)行某些操作。所有這些可以改變基于用戶的要求。本文讓我們看到倒數(shù)計時器可以在Java集合。
用Java CountDownTimer宣言
以下是Java中倒數(shù)計時器的聲明。
public abstract class CountDownTimer extends Object
倒數(shù)計時器有一個構(gòu)造函數(shù),如下描述。
CountDownTimer (long millisInFuture, long CountDownInterval)
millisInFuture:這個參數(shù)提到,米爾斯在未來的數(shù)當調(diào)用start()方法,直到onFinish()方法被調(diào)用。
countDownInterval:間隔onTick()回調(diào)。
下面提到的不同的方法:
1. 取消
public final void cancel ()
定義:這種方法有助于取消倒計時。
2. OnFinish
public final void onFinish ()
定義:這種方法有助于回調(diào)的時候。
3.onTick
public abstract void onTick ()
定義:這種方法有助于在定期的回調(diào)。
4. 開始
public final CountDownTimer start()
定義:這種方法有助于開始倒計時。
下面是步驟執(zhí)行Java的倒數(shù)計時器。
1. 開放的IDE,您想要創(chuàng)建一個Java文件。Eclipse IDE可以、Netbeans或JBuilder X,這取決于用戶的需求。
2. 導入包。主要進口所需的所有的時間在您的Java類文件的頂部。
3.設(shè)置倒計時時間。
4. 倒計時發(fā)生在毫秒。因此,確保變量也以毫秒為單位。如果你想計時器設(shè)置為5秒,“5000”必須提到,如下所示。
int cntdwn = 5000;
5. 設(shè)置定時器的一個實例。
timer = new Timer(cntdwn, this);
6. 使用Start()方法啟動計時器。
timer.start();
7. 您已經(jīng)創(chuàng)建了保存Java文件。
8. 編譯代碼和按下運行。
有一些方式可以設(shè)置倒計時定時器。讓我們來看看如何實現(xiàn)Java編程語言的幫助。
示例# 1
用Java程序來設(shè)置定時器
代碼:
import java.util.Timer;
import java.util.TimerTask;
//sample class
public class CountDownTimerExample {
//declare timer t
Timer t;
//constructor of the class
public CountDownTimerExample(int seconds) {
t = new Timer();
//schedule the timer
t.schedule(new rt(), seconds*1000);
}
//sub class that extends TimerTask
class rt extends TimerTask {
//task to perform on executing the program
public void run() {
System.out.println("Seconds you have input is over..!!! ");
t.cancel(); //stop the thread of timer
}
}
//main method
public static void main(String args[]) {
//pass 5 seconds as timer
new CountDownTimerExample(5);
System.out.println("Count down starts now!!! ");
}
}
輸出:
在執(zhí)行代碼,將顯示以下消息。
倒數(shù)計時器停止后,下面將顯示一條消息,表明時間設(shè)置為倒計時結(jié)束了。
例# 2
設(shè)置一個倒數(shù)計時器在Java的另一種方式
代碼:
//Java program to create a count down timer
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
//class
public class CountDownTimerExample {
//declare the interval i and timer t
static int i;
static Timer t;
//main method
public static void main(String[] args) {
//create object for scanner
Scanner sc = new Scanner(System.in);
// input the seconds you want to count down
System.out.print("Enter the seconds you want to count down : ");
//save the seconds that is input in to the variable sec
String sec = sc.nextLine();
//set delay and period as 1000
int del = 1000;
int per = 1000;
t = new Timer();
i = Integer.parseInt(sec);
System.out.println(sec);
//performs the specifiedd task at certain intervals
t.scheduleAtFixedRate(new TimerTask()
{
//task to be performed
public void run()
{
System.out.println(seti());
}
}, del, per);
}
//set interval
private static final int seti() {
//if interval is 1, cancel
if (i == 1)
t.cancel();
return --i;
}
}
輸出:
在這個程序中,用戶將被要求輸入的秒倒計時。
進入秒之后,我們將能夠看到倒計時顯示。
我們可以看到,3、2、1、0每秒鐘后顯示。
示例# 3
項目創(chuàng)建一個倒數(shù)計時器,每秒鐘后顯示一條消息。
代碼:
import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;
public class CountDownTimerExample {
//declare tk and t
Toolkit tk;
Timer t;
//constructor of CountDownTimerExample class
public CountDownTimerExample() {
tk = Toolkit.getDefaultToolkit();
t = new Timer();
//initial delay and subsequent rate
t.schedule(new rt(), 0, 1*1000);
}
class rt extends TimerTask {
//declare a variable beep
int beep = 3;
//task to be performed
public void run() {
//if BEEP VARIABLE IS GREATER THAN ZERO
if (beep > 0) {
//perform beep operation and print after each second
tk.beep();
System.out.println("One second over . . . Beep!");
//decrement the value beep
beep--;
}
//if beep variable is less than zero
else {
tk.beep();
System.out.println("The Time's over. . .!");
//AWT thread stops
System.exit(0);
}
}
}
public static void main(String args[]) {
System.out.println("Task is going to start. . .");
new CountDownTimerExample();
System.out.println("Task that is set up is scheduled. . .");
}
}
輸出:
可以看出,在執(zhí)行代碼,兩條消息顯示,如“任務(wù)開始。”和“任務(wù)設(shè)置計劃…”。在那之后,倒計時開始。
以上就是關(guān)于“Java倒數(shù)計時器的介紹”,大家如果想了解更多相關(guān)知識,可以關(guān)注一下動力節(jié)點的Java視頻教程,里面的課程內(nèi)容細致全面,通俗易懂,適合小白學習,希望對大家能夠有所幫助哦。