线程池示例(二)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

线程池的创建方式有7种,其中6种是通过`Executors`创建的,1种是通过`ThreadPoolExecutor`创建的。 以下是线程池的创建方式的介绍和示例代码: 1. `Executors.newFixedThreadPool`:创建一个固定大小的线程池,可控制并发的线程数,超出的线程会在队列中等待。 ```java ExecutorService executor = Executors.newFixedThreadPool(5); ``` 2. `Executors.newCachedThreadPool`:创建一个可缓存的线程池线程池的大小会根据需要进行自动扩展,但是在线程池中的线程空闲超过60秒后会被回收。 ```java ExecutorService executor = Executors.newCachedThreadPool(); ``` 3. `Executors.newSingleThreadExecutor`:创建一个单线程的线程池,保证所有任务按照指定顺序执行。 ```java ExecutorService executor = Executors.newSingleThreadExecutor(); ``` 4. `Executors.newScheduledThreadPool`:创建一个固定大小的线程池,可以延迟或定时执行任务。 ```java ScheduledExecutorService executor = Executors.newScheduledThreadPool(3); ``` 5. `Executors.newWorkStealingPool`:创建一个工作窃取线程池,每个线程都有自己的任务队列,当自己的任务队列为空时,会从其他线程的任务队列中窃取任务执行。 ```java ExecutorService executor = Executors.newWorkStealingPool(); ``` 6. `Executors.unconfigurableExecutorService`:创建一个不可配置的线程池,即无法修改线程池的配置参数。 ```java ExecutorService executor = Executors.unconfigurableExecutorService(executor); ``` 7. `ThreadPoolExecutor`:通过自定义参数创建线程池,可以灵活地配置线程池的大小、任务队列、拒绝策略等。 ```java ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, workQueue, handler); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值