select round(cast(date_part('epoch',to_timestamp((to_timestamp('2023-02-11 10:00:00','yyyy-mm-dd hh24:MI:SS')+ interval'1 day')::text,'yyyy-mm-dd hh24:MI:SS')
- to_timestamp((now() + interval '8 hour')::text,'yyyy-mm-dd hh24:MI:SS'))/60/60/24 as numeric),2)
as timeremaining ;
select round(cast(date_part('epoch',to_timestamp((start_time + interval'1 day')::text,'yyyy-mm-dd hh24:MI:SS')
- to_timestamp((now() + interval '8 hour')::text,'yyyy-mm-dd hh24:MI:SS'))/60/60/24 as numeric),3)
as timeremaining from company;
select round(cast(date_part('epoch',to_timestamp('2023:05:20 00:00:00'::text,'yyyy-mm-dd hh24:MI:SS')
-to_timestamp((now()+interval '8 hour')::text,'yyyy-mm-dd hh24:MI:SS'))/60/60/24 as numeric),2)
方式一:
application.yml
#线程池配置参数
task:
pool:
corePoolSize: 5 #设置核心线程数
maxPoolSize: 20 #设置最大线程数
keepAliveSeconds: 300 #设置线程活跃时间(秒)
queueCapacity: 50 #设置队列容量
启动类上加上开启线程池注解
@SpringBootApplication
@EnableAsync
@EnableConfigurationProperties({TaskThreadPoolConfig.class} ) // 开启配置属性支持
public class ThreadPoolApplication {
public static void main(String[] args) {
SpringApplication.run(ThreadPoolApplication.class, args);
}
}
线程池配置属性类TaskThreadPoolConfig .java
package com.threadpool.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* 线程池配置属性类
*/
@ConfigurationProperties(prefix = "task.pool")
@Data
public class TaskThreadPoolConfig {
private int corePoolSize;
private int maxPoolSize;
private int keepAliveSeconds;
private int queueCapacity;
}
创建线程池 TaskExecutePool .java
package com.threadpool.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* 创建线程池配置类
*/
@Configuration
public class TaskExecutePool {
@Autowired
private TaskThreadPoolConfig config;
/**
* 1.这种形式的线程池配置是需要在使用的方法上面@Async("taskExecutor"),
* 2.如果在使用的方法上面不加该注解那么spring就会使用默认的线程池
* 3.所以如果加@Async注解但是不指定使用的线程池,又想自己定义线程池那么就可以重写spring默认的线程池
* 4.所以第二个方法就是重写默认线程池
* 注意:完全可以把线程池的参数写到配置文件中
*/
@Bean("mytask")
public Executor taskExecutor() {
System.out.println("mymymymm");
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//核心线程池大小
executor.setCorePoolSize(config.getCorePoolSize());
//最大线程数
executor.setMaxPoolSize(config.getMaxPoolSize());
//队列容量
executor.setQueueCapacity(config.getQueueCapacity());
//活跃时间
executor.setKeepAliveSeconds(config.getKeepAliveSeconds());
//线程名字前缀
executor.setThreadNamePrefix("TaskExecutePool-");
// setRejectedExecutionHandler:当pool已经达到max size的时候,如何处理新任务
// CallerRunsPolicy:不在新线程中执行任务,而是由调用者所在的线程来执行
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 等待所有任务结束后再关闭线程池
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.initialize();
return executor;
}
}
在方法上添加@Async注解(可以指定线程池名字)
@GetMapping("/test2")
@ResponseBody
@Async("mytask")
public String findAllData2() throws ExecutionException, InterruptedException {
}
方式二:
package com.threadpool.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
@EnableAsync
@Configuration
public class ThreadPoolConfig {
/**
* 默认的线程池名称为myPoolExecutor
* 可以通过@Bean("name")方式进行指定
*/
@Bean
public TaskExecutor myPoolExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
// 核心线程数
taskExecutor.setCorePoolSize(6);
// 最大线程数
taskExecutor.setMaxPoolSize(10);
// 线程空间等待时间(秒)
taskExecutor.setKeepAliveSeconds(60);
// 任务队列大小
taskExecutor.setQueueCapacity(100);
// 线程前缀
taskExecutor.setThreadNamePrefix("my-thread");
// 拒绝策略
taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 等待所有任务结束后关闭线程池
taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
return taskExecutor;
}
}
//# 异步调用配置
//asyncThreadPool:
// # 核心线程数
// corePoolSize: 10
// # 最大线程数
// maxPoolSize: 20
// # 任务队列大小
// queueCapacity: 2
// # 缓冲队列中线程的空闲时间
// keepAliveSeconds: 60
// # 线程池中线程的名称前缀
// threadNamePrefix: asyncServiceExecutor-
///**
// * 线程池参数配置,多个线程池实现线程池隔离,@Async注解,默认使用系统自定义线程池,可在项目中设置多个线程池,在异步调用的时候,指明需要调用的线程池名称,比如:@Async("taskName")
// * @author dzh
// */
//@Configuration
//@Slf4j
//public class TaskPoolConfig {
//
// @Value("${asyncThreadPool.corePoolSize}")
// private int corePoolSize;
//
// @Value("${asyncThreadPool.maxPoolSize}")
// private int maxPoolSize;
//
// @Value("${asyncThreadPool.queueCapacity}")
// private int queueCapacity;
//
// @Value("${asyncThreadPool.keepAliveSeconds}")
// private int keepAliveSeconds;
//
// @Value("${asyncThreadPool.threadNamePrefix}")
// private String threadNamePrefix;
//
// @Bean("asyncExecutor")
// public Executor taskExecutor() {
//
// log.info("启动自定义线程池");
// ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// //核心线程池大小
// executor.setCorePoolSize(corePoolSize);
// //最大线程数
// executor.setMaxPoolSize(maxPoolSize);
// //配置队列容量
// executor.setQueueCapacity(queueCapacity);
// //活跃时间
// executor.setKeepAliveSeconds(keepAliveSeconds);
// //线程名字前缀
// executor.setThreadNamePrefix(threadNamePrefix);
// // 拒绝策略
// executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// // 线程初始化
// executor.initialize();
// return executor;
// }
@GetMapping("/test3")
@ResponseBody
@Async("myPoolExecutor")
public String findAllData3() throws ExecutionException, InterruptedException {
}