最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java編程倒計(jì)時(shí)實(shí)現(xiàn)方法示例

 更新時(shí)間:2017年09月08日 11:42:29   作者:Al_assad  
這篇文章主要介紹了Java編程倒計(jì)時(shí)實(shí)現(xiàn)的三個(gè)示例,三種實(shí)現(xiàn)方法,具有一定參考價(jià)值,需要的朋友可以了解下。

        實(shí)現(xiàn)Java編程中倒計(jì)時(shí)的方法有許多,下面我們通過(guò)三個(gè)示例來(lái)簡(jiǎn)單了解下它的實(shí)現(xiàn)過(guò)程。

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); 
 }*/ 
 
} 

總結(jié)

以上是本文的全部?jī)?nèi)容,希望對(duì)大家能有所幫助。

感謝大家對(duì)本站的支持。

相關(guān)文章

  • 淺析Java Web錯(cuò)誤/異常處理頁(yè)面

    淺析Java Web錯(cuò)誤/異常處理頁(yè)面

    這篇文章主要和大家一起對(duì)Java Web錯(cuò)誤/異常處理頁(yè)面進(jìn)行分析研究,感興趣的小伙伴們可以參考一下
    2016-03-03
  • Java注解Annotation解析

    Java注解Annotation解析

    這篇文章主要為大家詳細(xì)介紹了Java注解Annotation,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • spring和quartz整合,并簡(jiǎn)單調(diào)用(實(shí)例講解)

    spring和quartz整合,并簡(jiǎn)單調(diào)用(實(shí)例講解)

    下面小編就為大家?guī)?lái)一篇spring和quartz整合,并簡(jiǎn)單調(diào)用(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-07-07
  • Java多線程中的Executor詳解

    Java多線程中的Executor詳解

    這篇文章主要介紹了Java多線程中的Executor詳解,該接口提供了一種將任務(wù)提交與如何運(yùn)行每個(gè)任務(wù)的機(jī)制(包括線程使用、調(diào)度等細(xì)節(jié))解耦的方法,它通常使用預(yù)先創(chuàng)建線程而不是創(chuàng)建線程,需要的朋友可以參考下
    2023-12-12
  • 淺析Java IO相關(guān)知識(shí)點(diǎn)

    淺析Java IO相關(guān)知識(shí)點(diǎn)

    本篇文章給大家分享了關(guān)于java io的一些相關(guān)知識(shí)點(diǎn)以及相關(guān)內(nèi)容,對(duì)此有需要的朋友可以學(xué)習(xí)參考下。
    2018-05-05
  • Java?Agent?(代理)探針技術(shù)詳情

    Java?Agent?(代理)探針技術(shù)詳情

    這篇文章主要介紹了Java?Agent?探針技術(shù)詳情,Java?中的?Agent?技術(shù)可以讓我們無(wú)侵入性的去進(jìn)行代理,最常用于程序調(diào)試、熱部署、性能診斷分析等場(chǎng)景,下文更多相關(guān)資料,感興趣的小伙伴可以參考一下
    2022-04-04
  • springboot啟動(dòng)報(bào)錯(cuò):application?startup?failed問(wèn)題

    springboot啟動(dòng)報(bào)錯(cuò):application?startup?failed問(wèn)題

    這篇文章主要介紹了springboot啟動(dòng)報(bào)錯(cuò):application?startup?failed問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Java運(yùn)行時(shí)多態(tài)性的實(shí)現(xiàn)

    Java運(yùn)行時(shí)多態(tài)性的實(shí)現(xiàn)

    Java運(yùn)行時(shí)多態(tài)性的實(shí)現(xiàn)...
    2006-12-12
  • mybatis中的if?test判斷入?yún)⒌闹祮?wèn)題

    mybatis中的if?test判斷入?yún)⒌闹祮?wèn)題

    這篇文章主要介紹了mybatis中的if?test判斷入?yún)⒌闹祮?wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 詳解Maven 搭建spring boot多模塊項(xiàng)目(附源碼)

    詳解Maven 搭建spring boot多模塊項(xiàng)目(附源碼)

    這篇文章主要介紹了詳解Maven 搭建spring boot多模塊項(xiàng)目(附源碼),具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-09-09

最新評(píng)論

个旧市| 沙雅县| 商洛市| 招远市| 沙湾县| 喀喇沁旗| 康平县| 许昌县| 新乐市| 湘乡市| 米脂县| 衢州市| 松桃| 栾城县| 泸州市| 新竹市| 武威市| 林州市| 健康| 濮阳市| 和政县| 宁陵县| 盱眙县| 哈尔滨市| 岳普湖县| 高密市| 高陵县| 宿松县| 丹寨县| 偏关县| 墨脱县| 满城县| 江山市| 巴南区| 砀山县| 三台县| 哈巴河县| 米林县| 台南市| 奈曼旗| 呼和浩特市|