利用Java怎么編寫一個(gè)倒計(jì)時(shí)功能?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)專注于企業(yè)營銷型網(wǎng)站建設(shè)、網(wǎng)站重做改版、環(huán)翠網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、html5、商城開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為環(huán)翠等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
1.簡(jiǎn)易方式實(shí)現(xiàn)
/** * @see * @author Al_assad yulinying_1994@outlook.com * @date 2016年10月18日 上午3:10:13 * @version V1.0 * Description: 倒計(jì)時(shí)簡(jiǎn)易實(shí)現(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è)置倒計(jì)時(shí)間隔 } System.out.println("Time is out"); } //Test public static void main(String[] args) throws InterruptedException { new CountDown(100); //倒計(jì)時(shí)起始時(shí)間,多少秒 } }
2.使用ScheduleExecutor實(shí)現(xiàn)
/** * @see * @author Al_assad yulinying_1994@outlook.com * @date 2016年10月18日 上午2:14:43 * @version V1.0 * Description: 倒計(jì)時(shí)實(shí)現(xiàn)方式1:使用ScheduledExecutor實(shí)現(xiàn) * 使用兩個(gè)線程; */ import java.util.concurrent.*; public class CountDown1 { private volatile int limitSec ; //記錄倒計(jì)時(shí)時(shí)間 private int curSec; //記錄倒計(jì)時(shí)當(dāng)下時(shí)間 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實(shí)現(xiàn)
/** * @see * @author Al_assad yulinying_1994@outlook.com * @date 2016年10月18日 上午2:47:44 * @version V1.0 * Description: 倒計(jì)時(shí)實(shí)現(xiàn)方式2:使用java.uitl.Timer實(shí)現(xiàn) * 使用兩個(gè)線程 */ 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); }*/ }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
當(dāng)前名稱:利用Java怎么編寫一個(gè)倒計(jì)時(shí)功能
網(wǎng)頁地址:http://www.rwnh.cn/article48/psgchp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、網(wǎng)頁設(shè)計(jì)公司、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站制作、靜態(tài)網(wǎng)站、App設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)