任务调度ScheduledExecutorService

译:http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html

父类:ExecutorExecutorService

子类:ScheduledThreadPoolExecutor

方法:

1、schedule(Runnable command, long delay, TimeUnit unit)

作用:在delay延时后执行一次性command任务

参数:callable(要执行的任务),delay(延时的时长),unit(时长单位)

返回:ScheduledFuture待完成的任务,等任务完成后调用它的get()方法返回null值并抛异常

eg:

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class ScheduledExecutorTest implements Runnable {
    private String jobName = "";
    public ScheduledExecutorTest(String jobname) {
        super();
        this.jobName = jobname;
    }
    @Override
    public void run() {
        System.out.println("execute " + jobName);
    }
    public static void main(String[] args) {
        ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
        ScheduledFuture<?> able=service.schedule(new ScheduledExecutorTest("test1"), 1, TimeUnit.SECONDS);
        try {
            System.out.println(able.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
结果:

execute test1
null

2、schedule(Callable<V> callable, long delay, TimeUnit unit)

作用:在delay延时后执行一次性callable任务

参数:callable(要执行的任务),delay(延时的时长),unit(时长单位)


3、scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)

作用:任务第一次在初始化延时时间initialDelay后执行,然后在时间段initialDelay + period后执行,接着在时间段initialDelay + 2 * period后执行,依此类推。如果任务的任一执行遇到异常,后续执行就会取消。否则,任务将只能通过取消或执行程序终止而终止。如果这个任务的任一执行的时间比period长,则后续执行都会晚于实际的执行时间,但不会同时执行。

参数:callable(要执行的任务),delay(延时的时长),unit(时长单位)

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class ScheduledExecutorTest implements Runnable {
    private String jobName = "";
    public ScheduledExecutorTest(String jobname) {
        super();
        this.jobName = jobname;
    }
    @Override
    public void run() {
        System.out.println("execute " + jobName);
    }
    public static void main(String[] args) {
        ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
        service.scheduleAtFixedRate(new ScheduledExecutorTest("test1"), 1, 2, TimeUnit.SECONDS);
        service.scheduleWithFixedDelay(new ScheduledExecutorTest("test2"), 1, 1, TimeUnit.SECONDS);
        /*ScheduledFuture<?> able=service.schedule(new ScheduledExecutorTest("test1"), 1, TimeUnit.SECONDS);
        try {
            System.out.println(able.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }*/
    }
}


4、scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)

作用:任务第一次在初始化延时时间initialDelay后执行,第一个任务执行后经过delay时间后执行依此类推。如果任务的任一执行遇到异常,后续执行就会取消。否则,任务将只能通过取消或执行程序终止而终止。

参数:callable(要执行的任务),delay(延时的时长),unit(时长单位)


5、继承的父类ExecutorService方法有:awaitTerminationinvokeAllinvokeAllinvokeAnyinvokeAnyisShutdownisTerminatedshutdownshutdownNowsubmitsubmitsubmit

6、继承父类Executor方法execute


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值