Java并发 专题
您的位置:java > Java并发专题 > Java并发ScheduledThreadPoolExecutor类
Java并发ScheduledThreadPoolExecutor类
作者:--    发布时间:2019-11-20

java.util.concurrent.scheduledthreadpoolexecutorthreadpoolexecutor的子类,并且可以额外地调度在给定延迟之后运行的命令,或定期执行。

实例

以下testthread程序显示了基于线程的环境中scheduledthreadpoolexecutor接口的使用。

import java.util.concurrent.executors;
import java.util.concurrent.scheduledthreadpoolexecutor;
import java.util.concurrent.scheduledfuture;
import java.util.concurrent.timeunit;

public class testthread {

   public static void main(final string[] arguments) throws interruptedexception {
      final scheduledthreadpoolexecutor scheduler = (scheduledthreadpoolexecutor)executors.newscheduledthreadpool(1);

      final scheduledfuture<?> beephandler = 
         scheduler.scheduleatfixedrate(new beeptask(), 2, 2, timeunit.seconds);

      scheduler.schedule(new runnable() {
         @override
         public void run() {
            beephandler.cancel(true);
            scheduler.shutdown();            
         }
      }, 10, timeunit.seconds);
   }  

   static class beeptask implements runnable {
      public void run() {
         system.out.println("beep");      
      }
   }
}

这将产生以下结果 -

beep
beep
beep
beep
beep

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册