• ScheduledExecutorService 定时任务,线程


    java5 之后,并发线程部分增加了许多新的东西,新的启动、调度、管理线程的一大堆API,这时通过Executor来启动线程比Thread.start()更好,更容易控制线程的启动,销毁等,还可以使用线程池的功能。

    一.创建任务

    实际上就是实现Runnable接口,实现run方法。

    二.执行任务

    通过java.util.concurrent.ExecutorService接口对象来执行任务,该接口对象通过工具类java.util.concurrent.Executors的静态方法来创建。

    Executors此包中所定义的 Executor、ExecutorService、ScheduledExecutorService、ThreadFactory 和 Callable 类的工厂和实用方法。

    1、创建线程池(针对ScheduledExecutorService)

    private static ScheduledExecutorService singleScheduler = Executors.newScheduledThreadPool(4);

    2、添加任务到线程池中

    当将一个任务添加到线程池中,线程池会为每个任务分配一个线程去跑任务。执行任务时间可以定时,可以临时。

    (1)执行临时任务

    ScheduledExecutorService 有两个比较好的方法执行临时任务 execute(),submit()

    三个区别:

       a.参数不同

        execute() 只有一个方法,接收Runnable类,

        submit() 有三个重载方法,分别接收Runnable类,Callable类,和Runnable ,T(执行成功返回的值)

       b.返回值

        execute() 没有返回值,submit()有返回一个Future<> 执行后返回是否成功的标识。假如一个任务执行后,需要知道是否执行成功,如果失败,原因是什么。

       c.submit 方便异常处理

    (2)执行定时任务 scheduleAtFixedRate,scheduleWithFixedDelay 

    a.scheduleAtFixedRate

    具体参数说明

      public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay,long period,  TimeUnit unit);

      command 执行的线程;initialDelay 初始化延迟;period;两次开始执行最小时间间隔;unit 计时单位

    b.scheduleWithFixedDelay 

    具体参数说明

      public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay,long delay,  TimeUnit unit);

      command 执行的线程;initialDelay 初始化延迟;delay;前一次执行结束到下一次执行开始的间隔时间(间隔执行延迟时间);unit 计时单位

    三、关闭对象

      shutdown();

    四、示例

     1.简单的Runnable类

      excute()和submit()单个参数 的执行比较简单,不列。

      

    package test;
    
    public class ThreadDemo implements Runnable{
    
        public ThreadDemo(){
            
        }
        @Override
        public void run() {
            System.out.println("简单执行一下!");
        }
    }

      2.临时任务的执行

      

    package test;
    
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;
    import java.util.concurrent.ScheduledExecutorService;
    
    public class TestThread {
    
        private static ScheduledExecutorService singleScheduler =  Executors.newScheduledThreadPool(4);
        
        public static void main(String[] args) {
            Future<String> f =singleScheduler.submit(new ThreadDemo(),"成功!");
            try {
                System.out.println(f.get());    
            } catch (InterruptedException  e) {
                e.printStackTrace();
            }  catch (ExecutionException  e) {
                e.printStackTrace();
            }
            
        }
    } 

    返回结果

    简单执行一下!
    成功!

       3.定时任务的执行

    scheduleWithFixedDelay与此方法区别只是参数有些差异。

    package test;
    
    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.TimeUnit;
    
    public class TestThread {
    
        private static ScheduledExecutorService singleScheduler =  Executors.newScheduledThreadPool(4);
        
        public static void main(String[] args) {
            singleScheduler.scheduleAtFixedRate(new ThreadDemo(), 1, 1, TimeUnit.SECONDS);
        }
    } 

      返回结果

      

    简单执行一下!
    简单执行一下!
    简单执行一下!
    简单执行一下!
    简单执行一下!
  • 相关阅读:
    自适应兄弟元素一起增加高度
    replace小坑位一个
    word-wrap: break-word word-break: break-all;
    1473B. String LCM
    A. Special Permutation(水题)
    B. BerSU Ball(贪心)
    A. Regular Bracket Sequence(水题)
    B. Strange List(数学题)
    C. Move Brackets(水题)
    A. Flipping Game(暴力求法)
  • 原文地址:https://www.cnblogs.com/chfg/p/4832137.html
Copyright © 2020-2023  润新知