线程池示例(二)Executors.newScheduledThreadPool(1)

示例1 - 延迟执行

import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j
public class ThreadPoolExample4 {

    public static void main(String[] args) {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(int corePoolSize);

        scheduledExecutorService.schedule(new Runnable() {
            @Override
            public void run() {
                log.warn("schedule run");
            }
        }, 3, TimeUnit.SECONDS);
    }

}

输出:

17:40:36.288 [pool-1-thread-1] WARN com.example.concurrency.example.threadPool.ThreadPoolExample4 - schedule run

示例2 - 周期性执行

  • 延迟1秒后,每隔3秒执行一次;
  • 不适合理解shutdown(),需要等待一定的契机;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j
public class ThreadPoolExample4 {

    public static void main(String[] args) {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);

        scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                log.warn("schedule run");
            }
        }, 1, 3, TimeUnit.SECONDS);
    }

}

输出:

17:43:28.481 [pool-1-thread-1] WARN com.example.concurrency.example.threadPool.ThreadPoolExample4 - schedule run
17:43:31.478 [pool-1-thread-1] WARN com.example.concurrency.example.threadPool.ThreadPoolExample4 - schedule run
17:43:34.478 [pool-1-thread-1] WARN com.example.concurrency.example.threadPool.ThreadPoolExample4 - schedule run
17:43:37.478 [pool-1-thread-1] WARN com.example.concurrency.example.threadPool.ThreadPoolExample4 - schedule run

示例3 - 用Timer周期性执行

import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j
public class ThreadPoolExample4 {

    public static void main(String[] args) {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                log.warn("timer run");
            }
        }, new Date(), 5 * 1000);
    }
}

输出:

17:48:13.543 [Timer-0] WARN com.example.concurrency.example.threadPool.ThreadPoolExample4 - timer run
17:48:18.540 [Timer-0] WARN com.example.concurrency.example.threadPool.ThreadPoolExample4 - timer run
17:48:23.541 [Timer-0] WARN com.example.concurrency.example.threadPool.ThreadPoolExample4 - timer run

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值