這篇文章主要介紹了Java程式倒數實現的三個範例,三種實作方法,具有一定參考價值,需要的朋友可以了解下。
實現Java程式設計中倒數計時的方法有許多,下面我們透過三個範例來簡單了解下它的實作過程。
1.簡易方式實作
/** * @see * @author Al_assad yulinying_1994@outlook.com * @date 2016年10月18日 上午3:10:13 * @version V1.0 * Description: 倒计时简易实现,只用单线程 */ import java.util.*; import java.util.concurrent.*; public class CountDown { private int limitSec; public CountDown(int limitSec) throws InterruptedException{ this.limitSec = limitSec; System.out.println("Count from "+limitSec); while(limitSec > 0){ System.out.println("remians "+ --limitSec +" s"); TimeUnit.SECONDS.sleep(1); //设置倒计时间隔 } System.out.println("Time is out"); } //Test public static void main(String[] args) throws InterruptedException { new CountDown(100); //倒计时起始时间,多少秒 } }
2.使用ScheduleExecutor實作
/** * @see * @author Al_assad yulinying_1994@outlook.com * @date 2016年10月18日 上午2:14:43 * @version V1.0 * Description: 倒计时实现方式1:使用ScheduledExecutor实现 * 使用两个线程; */ import java.util.concurrent.*; public class CountDown1 { private volatile int limitSec ; //记录倒计时时间 private int curSec; //记录倒计时当下时间 public CountDown1(int limitSec) throws InterruptedException{ this.limitSec = limitSec; this.curSec = limitSec; System.out.println("count down form "+limitSec); ScheduledExecutorService exec = Executors.newScheduledThreadPool(1); exec.scheduleAtFixedRate(new Task(),0,1,TimeUnit.SECONDS); TimeUnit.SECONDS.sleep(limitSec); //暂停本线程 exec.shutdownNow(); System.out.println("Time out!"); } private class Task implements Runnable{ public void run(){ System.out.println("Time remains "+ --curSec +" s"); } } //Test /* public static void main(String[] args) throws InterruptedException{ new CountDown1(10); }*/ }
3.使用java.util.Timer實作
/** * @see * @author Al_assad yulinying_1994@outlook.com * @date 2016年10月18日 上午2:47:44 * @version V1.0 * Description: 倒计时实现方式2:使用java.uitl.Timer实现 * 使用两个线程 */ import java.util.*; import java.util.concurrent.TimeUnit; public class CountDown2 { private int limitSec; private int curSec; public CountDown2(int limitSec) throws InterruptedException{ this.limitSec = limitSec; this.curSec = limitSec; System.out.println("count down from "+limitSec+" s "); Timer timer = new Timer(); timer.schedule(new TimerTask(){ public void run(){ System.out.println("Time remians "+ --curSec +" s"); } },0,1000); TimeUnit.SECONDS.sleep(limitSec); timer.cancel(); System.out.println("Time is out!"); } //Test /* public static void main(String[] args) throws InterruptedException{ new CountDown2(10); }*/ }
總結
以上是Java實作倒數計時的方法詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA

本文解釋了用於構建分佈式應用程序的Java的遠程方法調用(RMI)。 它詳細介紹了接口定義,實現,註冊表設置和客戶端調用,以解決網絡問題和安全性等挑戰。

本文詳細介紹了用於網絡通信的Java的套接字API,涵蓋了客戶服務器設置,數據處理和關鍵考慮因素,例如資源管理,錯誤處理和安全性。 它還探索了性能優化技術,我

本文詳細介紹了創建自定義Java網絡協議。 它涵蓋協議定義(數據結構,框架,錯誤處理,版本控制),實現(使用插座),數據序列化和最佳實踐(效率,安全性,維護


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3 Linux新版
SublimeText3 Linux最新版

WebStorm Mac版
好用的JavaScript開發工具

禪工作室 13.0.1
強大的PHP整合開發環境

Atom編輯器mac版下載
最受歡迎的的開源編輯器