ScheduledThreadPoolExecutor

5313 단어 자바
public class ScheduledThreadPoolExecutor extends ThreadPoolExecutor implements ScheduledExecutorService {
public interface ScheduledExecutorService extends ExecutorService {
Scheduled Executor Service 는 네 개의 지연 또는 주기 작업 인터페이스 가 더 많아 졌 습 니 다.
//          ,    。        Runnable     ,
//    ScheduledFuture.get()     null
public ScheduledFuture> schedule(Runnable command,
                                       long delay, TimeUnit unit);
//          ,    。        Callable     ,
//  ,             
 public  ScheduledFuture schedule(Callable callable,
                                           long delay, TimeUnit unit);

//              ,period     ,
//             ,           ,
//         ,             ,                  
public ScheduledFuture> scheduleAtFixedRate(Runnable command,
                                                  long initialDelay,
                                                  long period,
                                                  TimeUnit unit);
//       initialDelay ,      。              
//    ,         delay。     ,       。
public ScheduledFuture> scheduleWithFixedDelay(Runnable command,
                                                     long initialDelay,
                                                     long delay,
                                                     TimeUnit unit);

쓰다
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(4);
        System.out.println("main " + new Date());

        ScheduledFuture> schedule = pool.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("schedule Runnable " + new Date());
            }
        }, 1, TimeUnit.SECONDS);
        System.out.println(schedule.get());

        ScheduledFuture schedule1 = pool.schedule(new Callable() {
            @Override
            public Integer call() throws Exception {
                System.out.println("schedule Callable " + new Date());
                return 1;
            }
        }, 2, TimeUnit.SECONDS);
        System.out.println(schedule1.get());

    }

main Wed Sep 04 11:07:42 CST 2019 schedule Runnable Wed Sep 04 11:07:43 CST 2019 null schedule Callable Wed Sep 04 11:07:45 CST 2019 1
scheduleAtFixed Rate 사용
1 초 지연 후, 첫 번 째 퀘 스 트 를 수행, 주기 2 초 후, 다시 퀘 스 트 를 수행
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(4);
        System.out.println("main " + new Date());

        ScheduledFuture> schedule = pool.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("schedule Runnable " + new Date());
            }
        }, 1, 2, TimeUnit.SECONDS);
        System.out.println(schedule.get());
        
    }

main Wed Sep 04 11:14:06 CST 2019 schedule Runnable Wed Sep 04 11:14:07 CST 2019 schedule Runnable Wed Sep 04 11:14:09 CST 2019 schedule Runnable Wed Sep 04 11:14:11 CST 2019
퀘 스 트 시간 3 초, 주기 시간 2 초 이상 이면 퀘 스 트 수행 완료 후 즉시 퀘 스 트 를 다시 수행 합 니 다.
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(4);
        System.out.println("main " + new Date());

        ScheduledFuture> schedule = pool.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("schedule Runnable " + new Date());
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, 1, 2, TimeUnit.SECONDS);
        System.out.println(schedule.get());

    }

main Wed Sep 04 11:16:12 CST 2019 schedule Runnable Wed Sep 04 11:16:13 CST 2019 schedule Runnable Wed Sep 04 11:16:16 CST 2019 schedule Runnable Wed Sep 04 11:16:19 CST 2019
scheduleWithFixedDelay 사용
퀘 스 트 수행 완료 후 2 초 지연, 퀘 스 트 다시 수행
   public static void main(String[] args) throws ExecutionException, InterruptedException {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(4);
        System.out.println("main " + new Date());

        ScheduledFuture> schedule = pool.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                System.out.println("schedule Runnable " + new Date());
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, 1, 2, TimeUnit.SECONDS);
        System.out.println(schedule.get());

    }

main Wed Sep 04 11:18:11 CST 2019 schedule Runnable Wed Sep 04 11:18:12 CST 2019 schedule Runnable Wed Sep 04 11:18:17 CST 2019 schedule Runnable Wed Sep 04 11:18:22 CST 2019

좋은 웹페이지 즐겨찾기