Algorithm
【leecode-35 搜索插入位置】
题目描述
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
你可以假设数组中无重复元素。
示例 1:
输入: [1,3,5,6], 5
输出: 2
示例 2:
输入: [1,3,5,6], 2
输出: 1
示例 3:
输入: [1,3,5,6], 7
输出: 4
示例 4:
输入: [1,3,5,6], 0
输出: 0
解题思路
排序数组中寻找目标值,可以选择二分查找法
代码实现
class Solution {
public int searchInsert(int[] nums, int target) {
int n = nums.length;
int left = 0, right = n - 1, ans = n;
while (left <= right) {
int mid = ((right - left) >> 1) + left;
if (target <= nums[mid]) {
ans = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
return ans;
}
}
Review
定时任务
(基于给定的时间点、时间间隔、给定的执行次数自动执行的任务)
定时任务的三种实现技术方案:
- Java自带的java.util.Timer类:传统调度方式,Timer工具类,通过执行TimerTask任务
- Quartz:功能比较强大的的调度器,可以让你的程序在指定时间执行,也可以按照某一个频度执行配置起来稍显复杂
- Spring Task:Spring3.0自带
Timer的使用
class MyTask extends TimerTask{
@Override
public void run() {
System.out.println("hello world");
}
}
public class TimerDemo {
public static void main(String[] args) {
//创建定时器对象
Timer t=new Timer();
//在3秒后执行MyTask类中的run方法,后面每10秒跑一次
t.schedule(new MyTask(), 3000,10000);
}
}
Timer:一种工具。线程用其安排以后在后台线程中运行的任务。可安排任务运行一次,或者定期反复运行。
TimerTask:实际执行任务。
理解:Timer是一种定时器工具,在后台线程调度运行指定任务,而TimerTask一个抽象类,它的子类代表一个能够被Timer调度的任务。
具体解释java定时任务
ScheduledThreadPoolExecutor的使用
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(8);
scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("hello world");
}
}, 1, 3, TimeUnit.SECONDS);
Springboot Task的使用
1、pom.xml 引入 web 依赖,需要用到 spring 的任务类
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2、入口类上加@EnableScheduling注解,激活定时任务
@SpringBootApplication
@EnableScheduling
public class SchedulingApplication {
public static void main(String[] args) {
SpringApplication.run(SchedulingApplication.class, args);
}
}
3、类上加@Component注解,被扫描到。方法上加@Scheduled注解,设置定时
@Component
public class TestTask {
// 3秒一次
@Scheduled(fixedRate = 3000)
public void doing() {
System.out.println("3秒一次");
}
// 6 秒一次:秒 分 时 日 月 周
@Scheduled(cron = "*/6 * * * * *")
public void doing2() {
System.out.println("6秒一次");
}
}
4、以上是同步定时任务的实现,但实际使用中很少会使用同步定时任务,一方面是会出现任务顺序执行的阻塞问题,还有一方面是同步定时任务是单线程的执行效率低。
5、多线程异步定时任务
@Configuration
@EnableAsync
public class AsyncTaskConfig {
@Bean
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 核心线程数量
executor.setCorePoolSize(2);
// 每个线程的最长存在时间
executor.setKeepAliveSeconds(1800);
// 最大的线程数量
executor.setMaxPoolSize(20);
// 缓冲队列
executor.setQueueCapacity(10);
executor.initialize();
return executor;
}
}
5、同步任务变异步任务
@Component
public class TestTask {
// 3秒一次
@Async
@Scheduled(fixedRate = 3000)
public void doing() {
System.out.println("3秒一次");
}
// 6 秒一次:秒 分 时 日 月 周
@Async
@Scheduled(cron = "*/6 * * * * *")
public void doing2() {
System.out.println("6秒一次");
}
}
其中cron的方式有:
- 每隔5秒执行一次:"*/5 * * * * ?"
- 每隔1分钟执行一次:“0 */1 * * * ?”
- 每天23点执行一次:“0 0 23 * * ?”
- 每天凌晨1点执行一次:“0 0 1 * * ?”
- 每月1号凌晨1点执行一次:“0 0 1 1 * ?”
- 每月最后一天23点执行一次:“0 0 23 L * ?”
- 每周星期天凌晨1点实行一次:“0 0 1 ? * L”
- 在26分、29分、33分执行一次:“0 26,29,33 * * * ?”
- 每天的0点、13点、18点、21点都执行一次:“0 0 0,13,18,21 * * ?”
- 表示在每月的1日的凌晨2点调度任务:“0 0 2 1 * ? *”
- 表示周一到周五每天上午10:15执行作业:“0 15 10 ? * MON-FRI”
- 表示2002-2006年的每个月的最后一个星期五上午10:15执行:“0 15 10 ? 6L 2002-2006”
参考:
SpringBoot的@Scheduled注解使用
定时任务并行处理
Tips
前后端传输Date类型数据时格式化日期
- 如果前后端传的数据都是json格式,那么后台接收数据的时候,传数据都可以用@JsonFormat
- @DateTimeFormat适合后端接收前端传来的参数,不管是不是json格式都可以正确转成Date型数据,只要前端传来的格式正确且后端@DateTimeFormat的pattern正确,但是,这个注解无法将Date型数据用json传到前端去.
方法一:
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private Date timingDate;
方法二:
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date timingDate;
方法三:
@Component
public class DateConverter implements Converter<String, Date>{
private static final String dataFormat = "yyyy-MM-dd HH:mm:ss";
private static final String shortDateFormat = "yyyy-MM-dd";
private static final String timeStampFormat = "^\\d+$";
private static final String hDateFormat = "yyyy年MM月dd日 HH:mm:ss";
private static final String hshortDateFormat = "yyyy年MM月dd日";
@override
public Date convert(String value){
if(StringUtils.isEmpty(value)){
return null;
}
value = value.trim();
try{
if (value.contains("-")) {
SimpleDateFormat formatter;
if (value.contains(":")) {
formatter = new SimpleDateFormat(dateFormat);
} else {
formatter = new SimpleDateFormat(shortDateFormat);
}
return formatter.parse(value);
} else if (value.matches(timeStampFormat)) {
Long lDate = new Long(value);
return new Date(lDate);
} else if (value.contains("年")) {
SimpleDateFormat formatter;
if (value.contains(":")) {
formatter = new SimpleDateFormat(hDateFormat);
} else {
formatter = new SimpleDateFormat(hshortDateFormat);
}
return formatter.parse(value);
}
}catch(Exception e){
throw new RuntimeException(String.format("parser %s to Date fail", value));
}
throw new RuntimeException(String.format("parser %s to Date fail", value));
}
}