实现Java编程中倒计时的方法有许多,下面我们通过三个示例来简单了解下它的实现过程。
1.简易方式实现
?
|
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: 倒计时简易实现,只用单线程
*/
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 yulinying_1994@outlook.com
* @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 yulinying_1994@outlook.com
* @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
相关文章
猜你喜欢
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 95
-
2025-06-04 94
-
2025-05-25 72
-
2025-05-25 97
-
2025-05-25 110
热门评论

