Java編程倒計時實現(xiàn)方法示例
實現(xiàn)Java編程中倒計時的方法有許多,下面我們通過三個示例來簡單了解下它的實現(xiàn)過程。
1.簡易方式實現(xiàn)
/** * @see * @author Al_assad yulinying_1994@outlook.com * @date 2016年10月18日 上午3:10:13 * @version V1.0 * Description: 倒計時簡易實現(xiàn),只用單線程 */ 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); //設(shè)置倒計時間隔 } System.out.println("Time is out"); } //Test public static void main(String[] args) throws InterruptedException { new CountDown(100); //倒計時起始時間,多少秒 } }
2.使用ScheduleExecutor實現(xiàn)
/** * @see * @author Al_assad yulinying_1994@outlook.com * @date 2016年10月18日 上午2:14:43 * @version V1.0 * Description: 倒計時實現(xiàn)方式1:使用ScheduledExecutor實現(xiàn) * 使用兩個線程; */ import java.util.concurrent.*; public class CountDown1 { private volatile int limitSec ; //記錄倒計時時間 private int curSec; //記錄倒計時當(dāng)下時間 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實現(xiàn)
/** * @see * @author Al_assad yulinying_1994@outlook.com * @date 2016年10月18日 上午2:47:44 * @version V1.0 * Description: 倒計時實現(xiàn)方式2:使用java.uitl.Timer實現(xiàn) * 使用兩個線程 */ 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); }*/ }
總結(jié)
以上是本文的全部內(nèi)容,希望對大家能有所幫助。
感謝大家對本站的支持。
相關(guān)文章
簡單理解Java的垃圾回收機(jī)制與finalize方法的作用
這篇文章主要介紹了簡單理解Java的垃圾回收機(jī)制與finalize方法的作用,著重講解了Java的GC銷毀對象的過程,需要的朋友可以參考下2015-11-11Intellij IDEA 配置Subversion插件實現(xiàn)步驟詳解
這篇文章主要介紹了Intellij IDEA 配置Subversion插件實現(xiàn)步驟詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05java實現(xiàn)一個接口調(diào)取另一個接口(接口一調(diào)取接口二)
這篇文章主要介紹了java實現(xiàn)一個接口調(diào)取另一個接口(接口一調(diào)取接口二),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09JAVA數(shù)據(jù)結(jié)構(gòu)之漢諾塔代碼實例
這篇文章主要介紹了JAVA數(shù)據(jù)結(jié)構(gòu)之漢諾塔,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Java?3年面試經(jīng)驗告訴你Mybatis是如何進(jìn)行分頁的
這篇文章主要介紹了Java?3年面試經(jīng)驗告訴你Mybatis是如何進(jìn)行分頁的,對于任何ORM框架,分頁的實現(xiàn)邏輯無外乎兩種,不管怎么包裝,最終給到開發(fā)者的,只是使用上的差異而已,本文給大家講解的很明白,感興趣的朋友一起看看吧2022-09-09一篇文章帶你入門Springboot沙箱環(huán)境支付寶支付(附源碼)
螞蟻沙箱環(huán)境 (Beta) 是協(xié)助開發(fā)者進(jìn)行接口功能開發(fā)及主要功能聯(lián)調(diào)的輔助環(huán)境。沙箱環(huán)境模擬了開放平臺部分產(chǎn)品的主要功能和主要邏輯2021-06-06