亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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í)的方法有許多,下面我們通過三個(gè)示例來簡(jiǎn)單了解下它的實(shí)現(xiàn)過程。

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)文章

最新評(píng)論