Java编程倒计时实现方法示例

 更新时间:2017年09月08日 11:42:29   作者:Al_assad  
这篇文章主要介绍了Java编程倒计时实现的三个示例,三种实现方法,具有一定参考价值,需要的朋友可以了解下。

        实现Java编程中倒计时的方法有许多,下面我们通过三个示例来简单了解下它的实现过程。

1.简易方式实现

/** 
* @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实现

/** 
* @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实现

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

总结

以上是本文的全部内容,希望对大家能有所帮助。

感谢大家对本站的支持。

相关文章

  • 浅析Java Web错误/异常处理页面

    浅析Java Web错误/异常处理页面

    这篇文章主要和大家一起对Java Web错误/异常处理页面进行分析研究,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • Java注解Annotation解析

    Java注解Annotation解析

    这篇文章主要为大家详细介绍了Java注解Annotation,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • spring和quartz整合,并简单调用(实例讲解)

    spring和quartz整合,并简单调用(实例讲解)

    下面小编就为大家带来一篇spring和quartz整合,并简单调用(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • Java多线程中的Executor详解

    Java多线程中的Executor详解

    这篇文章主要介绍了Java多线程中的Executor详解,该接口提供了一种将任务提交与如何运行每个任务的机制(包括线程使用、调度等细节)解耦的方法,它通常使用预先创建线程而不是创建线程,需要的朋友可以参考下
    2023-12-12
  • 浅析Java IO相关知识点

    浅析Java IO相关知识点

    本篇文章给大家分享了关于java io的一些相关知识点以及相关内容,对此有需要的朋友可以学习参考下。
    2018-05-05
  • Java Agent (代理)探针技术详情

    Java Agent (代理)探针技术详情

    这篇文章主要介绍了Java Agent 探针技术详情,Java 中的 Agent 技术可以让我们无侵入性的去进行代理,最常用于程序调试、热部署、性能诊断分析等场景,下文更多相关资料,感兴趣的小伙伴可以参考一下
    2022-04-04
  • springboot启动报错:application startup failed问题

    springboot启动报错:application startup failed问题

    这篇文章主要介绍了springboot启动报错:application startup failed问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-07-07
  • Java运行时多态性的实现

    Java运行时多态性的实现

    Java运行时多态性的实现...
    2006-12-12
  • mybatis中的if test判断入参的值问题

    mybatis中的if test判断入参的值问题

    这篇文章主要介绍了mybatis中的if test判断入参的值问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • 详解Maven 搭建spring boot多模块项目(附源码)

    详解Maven 搭建spring boot多模块项目(附源码)

    这篇文章主要介绍了详解Maven 搭建spring boot多模块项目(附源码),具有一定的参考价值,有兴趣的可以了解一下
    2017-09-09

最新评论