package com.da.tool.guava;

import com.google.common.util.concurrent.AbstractScheduledService;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 */
public class SchedulerJob extends AbstractScheduledService {

    private List<String> list;

    private static Boolean isEnd = false;

    @Override
    protected void runOneIteration() throws Exception {
        System.out.println("Add element to list ..........");
        list.add("test");
        if(list.size()>10){
            System.out.println("ShutDown job ..........");
            isEnd = true;
            this.stopAndWait();
        }
    }

    @Override
    protected void startUp() throws Exception {
        System.out.println("Job start ..........");
        list = new ArrayList<>();
    }

    @Override
    protected void shutDown() throws Exception {
        System.out.println("Job end ..........");
    }

    @Override
    protected Scheduler scheduler() {
        return Scheduler.newFixedRateSchedule(0,1, TimeUnit.SECONDS);
    }

    public static void main(String[] args) {
        SchedulerJob schedulerJob =new SchedulerJob();
        try {
            schedulerJob.startAndWait();
        } catch (Exception e) {
            e.printStackTrace();
        }
        while(!isEnd){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.getStackTrace();
            }
        }
        System.exit(0);
    }
}

 

相关文章:

  • 2022-12-23
  • 2022-01-18
  • 2022-01-22
  • 2021-07-14
  • 2022-03-02
  • 2021-08-03
  • 2021-09-10
  • 2022-12-23
猜你喜欢
  • 2021-08-21
  • 2022-02-08
  • 2022-12-23
  • 2022-12-23
  • 2022-02-09
  • 2022-01-10
  • 2021-12-13
相关资源
相似解决方案