java实现定时任务的三种方法

2018-01-17 15:58:16

[java] view plain copy

  1. /** 

  2.  * 普通thread 

  3.  * 这是最常见的,创建一个thread,然后让它在while循环里一直运行着, 

  4.  * 通过sleep方法来达到定时任务的效果。这样可以快速简单的实现,代码如下: 

  5.  * @author GT 

  6.  * 

  7.  */  

  8. public class Task1 {  

  9.     public static void main(String[] args) {  

  10.         // run in a second  

  11.         final long timeInterval = 1000;  

  12.         Runnable runnable = new Runnable() {  

  13.             public void run() {  

  14.                 while (true) {  

  15.                     // ------- code for task to run  

  16.                     System.out.println("Hello !!");  

  17.                     // ------- ends here  

  18.                     try {  

  19.                         Thread.sleep(timeInterval);  

  20.                     } catch (InterruptedException e) {  

  21.                         e.printStackTrace();  

  22.                     }  

  23.                 }  

  24.             }  

  25.         };  

  26.         Thread thread = new Thread(runnable);  

  27.         thread.start();  

  28.     }  

  29. }  



[java] view plain copy

  1. import java.util.Timer;  

  2. import java.util.TimerTask;  

  3.   

  4. /** 

  5.  *  

  6.  * 于第一种方式相比,优势 1>当启动和去取消任务时可以控制 2>第一次执行任务时可以指定你想要的delay时间 

  7.  *  

  8.  * 在实现时,Timer类可以调度任务,TimerTask则是通过在run()方法里实现具体任务。 Timer实例可以调度多任务,它是线程安全的。 

  9.  * 当Timer的构造器被调用时,它创建了一个线程,这个线程可以用来调度任务。 下面是代码: 

  10.  *  

  11.  * @author GT 

  12.  *  

  13.  */  

  14. public class Task2 {  

  15.     public static void main(String[] args) {  

  16.         TimerTask task = new TimerTask() {  

  17.             @Override  

  18.             public void run() {  

  19.                 // task to run goes here  

  20.                 System.out.println("Hello !!!");  

  21.             }  

  22.         };  

  23.         Timer timer = new Timer();  

  24.         long delay = 0;  

  25.         long intevalPeriod = 1 * 1000;  

  26.         // schedules the task to be run in an interval  

  27.         timer.scheduleAtFixedRate(task, delay, intevalPeriod);  

  28.     } // end of main  

  29. }  



[java] view plain copy

  1. import java.util.concurrent.Executors;  

  2. import java.util.concurrent.ScheduledExecutorService;  

  3. import java.util.concurrent.TimeUnit;  

  4.   

  5. /** 

  6.  *  

  7.  *  

  8.  * ScheduledExecutorService是从Java SE5的java.util.concurrent里,做为并发工具类被引进的,这是最理想的定时任务实现方式。  

  9.  * 相比于上两个方法,它有以下好处: 

  10.  * 1>相比于Timer的单线程,它是通过线程池的方式来执行任务的  

  11.  * 2>可以很灵活的去设定第一次执行任务delay时间 

  12.  * 3>提供了良好的约定,以便设定执行的时间间隔 

  13.  *  

  14.  * 下面是实现代码,我们通过ScheduledExecutorService#scheduleAtFixedRate展示这个例子,通过代码里参数的控制,首次执行加了delay时间。 

  15.  *  

  16.  *  

  17.  * @author GT 

  18.  *  

  19.  */  

  20. public class Task3 {  

  21.     public static void main(String[] args) {  

  22.         Runnable runnable = new Runnable() {  

  23.             public void run() {  

  24.                 // task to run goes here  

  25.                 System.out.println("Hello !!");  

  26.             }  

  27.         };  

  28.         ScheduledExecutorService service = Executors  

  29.                 .newSingleThreadScheduledExecutor();  

  30.         // 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间  

  31.         service.scheduleAtFixedRate(runnable, 101, TimeUnit.SECONDS);  

  32.     }  

  33. }  



  • 2020-12-18 17:15:29

    coTurn stun服务器搭建,禁用turn

    https://github.com/coturn/coturn 在这里git clone 下来然后编译安装,一切默认即可。编译后,也可以不用安装。在编译目录下bin文件夹下有turnserver turnutils_stunclient turnutils_uclient 这三个等一下会用到的软件。

  • 2020-12-18 17:26:25

    coturn配置文件详细解释

    Coturn 是webrtc,p2p视频通话必不少的,主要包含2个主要功能stun服务, turn服务 Coturn 的githup地址为 https://github.com/coturn/coturn/

  • 2020-12-21 06:26:16

    为UIView添加点击事件

    最近经常碰到要将UIImageView和UILabel看成整体的情况,我于是就将他俩用UIView包起来,那么怎么给一个UIView添加点击事件,可以这么实现:

  • 2020-12-21 09:00:20

    Window.matchMedia() 方法详解

    matchMedia() 返回一个新的 MediaQueryList 对象,表示指定的媒体查询字符串解析后的结果。 matchMedia() 方法的值可以是任何一个 CSS @media 规则 的特性, 如 min-height, min-width, orientation 等。 MediaQueryList 对象有以下两个属性:

  • 2020-12-21 09:42:42

    iframe.contentWindow 操作iframe

    注:iframe.contentWindow这里,返回的是iframe的window对象,所以后面可以接着调用document方法,再接着调用getElementByTagName。那么就可以对iframe里面的元素进行操作了。

  • 2020-12-21 14:00:19

    iframe + postMessage跨域通信

    在实际项目开发中可能会碰到在 a.com 页面中嵌套 b.com 页面,这时第一反应是使用 iframe,但是产品又提出在 a.com 中操作,b.com 中进行显示,或者相反。

  • 2020-12-22 12:02:41

    ios开发优秀的开源框架,demo集合

    期待大家和我们一起共同维护,同时也期望大家随时能提出宝贵的意见(直接提交issues即可)。请广大网友只按照目录结构(即使目录结构有问题)添加三方库,并提交pull request。目录问题大家提出issues后楼主会及时更改的。