国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

香港云服务器
服務(wù)器之家 - 編程語言 - Java教程 - Java編程倒計(jì)時(shí)實(shí)現(xiàn)方法示例

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

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

這篇文章主要介紹了Java編程倒計(jì)時(shí)實(shí)現(xiàn)的三個(gè)示例,三種實(shí)現(xiàn)方法,具有一定參考價(jià)值,需要的朋友可以了解下。

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

1.簡易方式實(shí)現(xiàn)

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 yulinying_1994@outlook.com
* @date 2016年10月18日 上午3:10:13
* @version V1.0
* Description: 倒計(jì)時(shí)簡易實(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)

?
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 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)

?
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 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é)

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

感謝大家對本站的支持。

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

延伸 · 閱讀

精彩推薦
488
主站蜘蛛池模板: 国产精品久久久久一区二区三区 | 在线成人av | 亚洲免费a | 久久一| 亚洲成人久久久久 | 亚洲一区二区三区免费观看 | 日本一区二区三区在线视频 | 国产黄色在线观看 | 依人成人网| 国产欧美日韩在线观看 | 亚洲视频二区 | 成年人毛片视频 | av私库在线观看 | av网站在线播放 | 高清视频一区 | 韩国精品一区二区三区 | 午夜视频在线 | 国产日韩视频 | 国产一级毛片一级 | 人人爽视频 | 精品九九| 精品一区二区不卡 | 亚洲视频中文字幕 | 国产精品第一国产精品 | 国产99在线 | 亚洲 | 成人不卡在线观看 | 亚洲一区二区三区久久久 | 青青草综合 | 国产精品视频一区二区三区不卡 | 黄色成人在线 | 久久久亚洲国产美女国产盗摄 | av观看免费 | 欧美日韩精品久久久 | 在线激情视频 | 日韩欧美一区二区三区 | 毛片网免费 | 欧美日韩不卡合集视频 | 五月婷婷精品 | 久久久av | 国产精品不卡一区二区三区 | 亚洲国产精品一区二区久久 |