更新時(shí)間:2022-09-28 15:42:01 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽3040次
在Java中,有幾種情況對(duì)時(shí)間敏感的任務(wù),比如倒計(jì)時(shí)定時(shí)器被添加到Java形式。在這些計(jì)時(shí)器的時(shí)間,直到一個(gè)函數(shù)可以設(shè)置由用戶觸發(fā)。它將持續(xù)運(yùn)行的情況下必須連續(xù)觸發(fā)的函數(shù)。當(dāng)達(dá)到計(jì)數(shù)停機(jī)時(shí),可以重置計(jì)時(shí)器。內(nèi)置的Java包可用于設(shè)置的時(shí)間和定期執(zhí)行某些操作。所有這些可以改變基于用戶的要求。本文讓我們看到倒數(shù)計(jì)時(shí)器可以在Java集合。
用Java CountDownTimer宣言
以下是Java中倒數(shù)計(jì)時(shí)器的聲明。
public abstract class CountDownTimer extends Object
倒數(shù)計(jì)時(shí)器有一個(gè)構(gòu)造函數(shù),如下描述。
CountDownTimer (long millisInFuture, long CountDownInterval)
millisInFuture:這個(gè)參數(shù)提到,米爾斯在未來(lái)的數(shù)當(dāng)調(diào)用start()方法,直到onFinish()方法被調(diào)用。
countDownInterval:間隔onTick()回調(diào)。
下面提到的不同的方法:
1. 取消
public final void cancel ()
定義:這種方法有助于取消倒計(jì)時(shí)。
2. OnFinish
public final void onFinish ()
定義:這種方法有助于回調(diào)的時(shí)候。
3.onTick
public abstract void onTick ()
定義:這種方法有助于在定期的回調(diào)。
4. 開(kāi)始
public final CountDownTimer start()
定義:這種方法有助于開(kāi)始倒計(jì)時(shí)。
下面是步驟執(zhí)行Java的倒數(shù)計(jì)時(shí)器。
1. 開(kāi)放的IDE,您想要?jiǎng)?chuàng)建一個(gè)Java文件。Eclipse IDE可以、Netbeans或JBuilder X,這取決于用戶的需求。
2. 導(dǎo)入包。主要進(jìn)口所需的所有的時(shí)間在您的Java類(lèi)文件的頂部。
3.設(shè)置倒計(jì)時(shí)時(shí)間。
4. 倒計(jì)時(shí)發(fā)生在毫秒。因此,確保變量也以毫秒為單位。如果你想計(jì)時(shí)器設(shè)置為5秒,“5000”必須提到,如下所示。
int cntdwn = 5000;
5. 設(shè)置定時(shí)器的一個(gè)實(shí)例。
timer = new Timer(cntdwn, this);
6. 使用Start()方法啟動(dòng)計(jì)時(shí)器。
timer.start();
7. 您已經(jīng)創(chuàng)建了保存Java文件。
8. 編譯代碼和按下運(yùn)行。
有一些方式可以設(shè)置倒計(jì)時(shí)定時(shí)器。讓我們來(lái)看看如何實(shí)現(xiàn)Java編程語(yǔ)言的幫助。
示例# 1
用Java程序來(lái)設(shè)置定時(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ù)計(jì)時(shí)器停止后,下面將顯示一條消息,表明時(shí)間設(shè)置為倒計(jì)時(shí)結(jié)束了。

例# 2
設(shè)置一個(gè)倒數(shù)計(jì)時(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;
}
}
輸出:
在這個(gè)程序中,用戶將被要求輸入的秒倒計(jì)時(shí)。

進(jìn)入秒之后,我們將能夠看到倒計(jì)時(shí)顯示。

我們可以看到,3、2、1、0每秒鐘后顯示。
示例# 3
項(xiàng)目創(chuàng)建一個(gè)倒數(shù)計(jì)時(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ù)開(kāi)始。”和“任務(wù)設(shè)置計(jì)劃…”。在那之后,倒計(jì)時(shí)開(kāi)始。
以上就是關(guān)于“Java倒數(shù)計(jì)時(shí)器的介紹”,大家如果想了解更多相關(guān)知識(shí),可以關(guān)注一下動(dòng)力節(jié)點(diǎn)的Java視頻教程,里面的課程內(nèi)容細(xì)致全面,通俗易懂,適合小白學(xué)習(xí),希望對(duì)大家能夠有所幫助哦。
相關(guān)閱讀
Java實(shí)驗(yàn)班
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
Java就業(yè)班
有基礎(chǔ) 直達(dá)就業(yè)
Java夜校直播班
業(yè)余時(shí)間 高薪轉(zhuǎn)行
Java在職加薪班
工作1~3年,加薪神器
Java架構(gòu)師班
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)