志宇-springboot小知识点

定时任务的使用

1、启动类上添加@EnableScheduling,开启定时任务
2、Scheduled参数说明
cron:cron表达式,指定任务在特定时间执行;
fixedDelay:表示上一次任务执行完成后多久再次执行,参数类型为long,单位ms;
fixedRate:表示按一定的频率执行任务,参数类型为long,单位ms;
initialDelay:表示延迟多久再第一次执行任务,参数类型为long,单位ms;
3、配置定时任务执行为多线程及使用如下

@Configuration
@EnableScheduling
public class TestSchedule implements SchedulingConfigurer {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH:mm:ss");

    @Scheduled(cron = "0/5 * * * * ?")
    public void testSchedule1() throws InterruptedException {
        String dateTime = LocalDateTime.now(ZoneOffset.of("+8")).format(formatter);//字符串转时间String dateTimeStr = "2018-07-28 14:11:15";
        System.out.println(Thread.currentThread()+"等上一次执行完,再等5秒钟在执行>>"+ dateTime);
        Thread.sleep(10000L);
    }

    @Scheduled(fixedDelay=4000)
    private void testSchedule3() throws InterruptedException {
        String dateTime = LocalDateTime.now(ZoneOffset.of("+8")).format(formatter);
        System.out.println(Thread.currentThread()+"等上次执行完再等两秒再执行>>"+ dateTime);
        Thread.sleep(2000L);
    }

    @Scheduled(fixedRate=2000)
    public void testSchedule4() throws InterruptedException {
        String dateTime = LocalDateTime.now(ZoneOffset.of("+8")).format(formatter);
        System.out.println(Thread.currentThread()+"每次两秒执行一次>>"+ dateTime);
        System.out.println(Thread.currentThread()+"执行时候如果上一次已经执行结束,则直接执行>>"+ dateTime);
        System.out.println(Thread.currentThread()+"执行时候如果上一次没执行结束,等执行结束立即执行>>"+ dateTime);
        Thread.sleep(4000L);
    }

    @Scheduled(initialDelay = 10000,fixedRate = 15000)
    private void testSchedule5(){
        String dateTime = LocalDateTime.now(ZoneOffset.of("+8")).format(formatter);
        System.out.println("项目启动10秒后再执行,每15秒固定执行一次>>"+ dateTime);
    }

    //设置定时任务执行为多线程的
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        //设置线程核数
        taskRegistrar.setScheduler(Executors.newScheduledThreadPool(10));
    }
}

springboot中异步方法使用

1、在启动类上@EnableAsync 开启异步执行任务

@ServletComponentScan
@SpringBootApplication
@EnableAsync
public class ActivityApplication {
	public static void main(String[] args) {
		try {
			SpringApplication.run(ActivityApplication.class, args);
		} catch (Throwable e) {
			throw new RuntimeException(e);
		}
	}
}

2、在要异步执行的方法或类上加@Async注解

public interface ActivityLogInter {    
    Future<String> test() throws InterruptedException;
}

@Service
@Slf4j
public class ActivityLogImpl  implements ActivityLogInter {
    @Override
    @Async
    public Future<String> test() throws InterruptedException {
		System.out.println("异步方法执行中");
        //测试异步的使用
        Thread.sleep(2000l);
		Future<String> resultFuture=new AsyncResult<>("222");
		System.out.println("异步方法执行完");
        return resultFuture;
    }
}

3、将对象注入后异步执行方法,阻塞获得返回值

@RestController
@RequestMapping("/test1")
@Slf4j
public class Test1Controller {
    @Autowired
    private ActivityLogInter activityLogImpl;
    
    @GetMapping(value = "/time")
    @ResponseBody
    public void test() throws InterruptedException {
        //这个方法是异步执行的
        Future<String> future = activityLogImpl.test();
        System.out.println("执行业务逻辑");
        //阻塞获得返回值
		String s = future.get();
		System.out.println(s);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值