更新時間:2022-09-28 15:42:01 來源:動力節點 瀏覽2470次
在Java中,有幾種情況對時間敏感的任務,比如倒計時定時器被添加到Java形式。在這些計時器的時間,直到一個函數可以設置由用戶觸發。它將持續運行的情況下必須連續觸發的函數。當達到計數停機時,可以重置計時器。內置的Java包可用于設置的時間和定期執行某些操作。所有這些可以改變基于用戶的要求。本文讓我們看到倒數計時器可以在Java集合。
用Java CountDownTimer宣言
以下是Java中倒數計時器的聲明。
public abstract class CountDownTimer extends Object
倒數計時器有一個構造函數,如下描述。
CountDownTimer (long millisInFuture, long CountDownInterval)
millisInFuture:這個參數提到,米爾斯在未來的數當調用start()方法,直到onFinish()方法被調用。
countDownInterval:間隔onTick()回調。
下面提到的不同的方法:
1. 取消
public final void cancel ()
定義:這種方法有助于取消倒計時。
2. OnFinish
public final void onFinish ()
定義:這種方法有助于回調的時候。
3.onTick
public abstract void onTick ()
定義:這種方法有助于在定期的回調。
4. 開始
public final CountDownTimer start()
定義:這種方法有助于開始倒計時。
下面是步驟執行Java的倒數計時器。
1. 開放的IDE,您想要創建一個Java文件。Eclipse IDE可以、Netbeans或JBuilder X,這取決于用戶的需求。
2. 導入包。主要進口所需的所有的時間在您的Java類文件的頂部。
3.設置倒計時時間。
4. 倒計時發生在毫秒。因此,確保變量也以毫秒為單位。如果你想計時器設置為5秒,“5000”必須提到,如下所示。
int cntdwn = 5000;
5. 設置定時器的一個實例。
timer = new Timer(cntdwn, this);
6. 使用Start()方法啟動計時器。
timer.start();
7. 您已經創建了保存Java文件。
8. 編譯代碼和按下運行。
有一些方式可以設置倒計時定時器。讓我們來看看如何實現Java編程語言的幫助。
示例# 1
用Java程序來設置定時器
代碼:
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!!! ");
}
}
輸出:
在執行代碼,將顯示以下消息。
倒數計時器停止后,下面將顯示一條消息,表明時間設置為倒計時結束了。
例# 2
設置一個倒數計時器在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
項目創建一個倒數計時器,每秒鐘后顯示一條消息。
代碼:
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. . .");
}
}
輸出:
可以看出,在執行代碼,兩條消息顯示,如“任務開始。”和“任務設置計劃…”。在那之后,倒計時開始。
以上就是關于“Java倒數計時器的介紹”,大家如果想了解更多相關知識,可以關注一下動力節點的Java視頻教程,里面的課程內容細致全面,通俗易懂,適合小白學習,希望對大家能夠有所幫助哦。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習