一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java編程倒計時實現方法示例

Java編程倒計時實現方法示例

2020-12-30 11:34Al_assad Java教程

這篇文章主要介紹了Java編程倒計時實現的三個示例,三種實現方法,具有一定參考價值,需要的朋友可以了解下。

        實現Java編程中倒計時的方法有許多,下面我們通過三個示例來簡單了解下它的實現過程。

1.簡易方式實現

java" id="highlighter_150924">
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
* @see
* @author Al_assad [email protected]
* @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實現

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* @see
* @author Al_assad [email protected]
* @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實現

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* @see
* @author Al_assad [email protected]
* @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);
 }*/
 
}

總結

以上是本文的全部內容,希望對大家能有所幫助。

感謝大家對本站的支持。

原文鏈接:http://blog.csdn.net/al_assad/article/details/52992875

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩不卡一区二区三区 | 亚洲乱码一二三四五六区 | 四虎成人国产精品视频 | 日日操综合 | 成人高清网站 | 欧美日韩精品亚洲精品v18 | 消息称老熟妇乱视频一区二区 | 亚洲狠狠婷婷综合久久久久网站 | 久九九精品免费视频 | 91免费播放人人爽人人快乐 | 欧美男同猛男 videos 同性 | 天堂8在线天堂资源在线 | 5g影院天天影院天天爽影院网站 | jazz欧美人免费xxxxxx | www.99精品视频在线播放 | 亚洲天堂男人 | 国产午夜久久精品 | eeuss18影院www国产 | 我被黑人彻底征服的全文 | 精品福利视频一区二区三区 | 亚洲天堂岛国片 | 午夜精品久久久内射近拍高清 | 亚洲成人中文 | 天天操天天干天天做 | 99视频久久 | 娜娜啪| 国产成人精品系列在线观看 | 国产精品 色 | 欧美一卡2卡三卡4卡5卡免费观看 | 全黄h全肉细节修仙玄幻文 全彩调教侵犯h本子全彩妖气he | 人妖巨茎video | 欧美brazzers | 国产va免费精品高清在线 | 91噜噜噜在线观看 | tiny4k欧美极品在线 | 男人叼女人的痛爽视频免费 | 亚洲一成人毛片 | 欧美综合精品一区二区三区 | 热久久免费 | 免费国产一级 | 男女天堂 |