Java之@Schedule与@Async注解失效总结

Schedule注解失效

Schedule标记的方法的类没有被spring托管

当@Scheduled方法所属的类没有使用@Component,@Service,@Controller等修饰的时候定时任务不执行,当两个定时任务同时执行时,两个定时任务随机先后执行

/**
 * 当前类不交给spring托管,定时任务失效
 * 两个定时任务同一个时间点,谁先抢到锁谁先执行
 */
@Component
public class ScheduleOne {
    @Scheduled(cron = "0 22 * * * ? ")
    public void test1() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("子线程名:"+Thread.currentThread().getName());
            System.out.println(String.format("第%d次执行任务一",i));
        }
    }
    @Scheduled(cron = "0 22 * * * ? ")
    public void test2() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("子线程名:"+Thread.currentThread().getName());
            System.out.println(String.format("第%d次执行任务二",i));
        }
    }
}

在这里插入图片描述

@Async注解失效

异步任务等同于开启了一个线程池,也可以自定义线程池
在这里插入图片描述

同类中调用异步方法,异步功能失效

    @Async("asyncExecutors")
    public void java() {
        System.out.println("-----------------------------------------------------elegant separator-----------------------------------------------------------------");
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(1000);
                System.out.println("Async++++++++子线程java名:"+Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(i + "hello java");
        }
        System.out.println("-----------------------------------------------------elegant separator-----------------------------------------------------------------");
    }
    @Test
    public void getJava() {
        java();
        System.out.println("异步任务执行成功了吗");
    }

执行结果:
在这里插入图片描述

通过测试方法调用异步方法异步失效

    @Async
    public void python() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(1000);
                System.out.println("Async++++++++子线程java名:"+Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(i + "hello python");
        }
    }
    
    @Test
    public void getPython() {

        PrintOne printOne = new PrintOne();
        printOne.python();
        System.out.println("异步任务执行成功了吗");
    }

在这里插入图片描述

通过new对象调用异步方法异步失效

    @RequestMapping(method = RequestMethod.GET,value = "three")
    public String test3() {
        PrintOne printOne1 = new PrintOne();
        printOne1.python();
        System.out.println("主线程名"+ Thread.currentThread().getName());
        return "方法返回值";
    }

在这里插入图片描述

测试类中异步方法压根不会进入


@SpringBootTest
@RunWith(SpringRunner.class)
public class AsyncTest {
    /**
     * 测试类中异步方法不执行
     */
    @Resource
    PrintOne printOne;
    @Test
    public void test1() {
        printOne.python();
        System.out.println("异步任务执行了吗");
    }
}

在这里插入图片描述

类被spring托管,且使用自动装配的方式调用才可以生效,且不在测试类中注解生效

    @RequestMapping(method = RequestMethod.GET,value = "one")
    public String test2() {
        printOne.java();
        System.out.println("主线程名"+ Thread.currentThread().getName());
        return "方法返回值";
    }

执行结果
在这里插入图片描述

@Schedule与@Async同时存在

两个任务可以交替执行

@Service
public class ScheduledThree {
    @Scheduled(cron = "0 45 * * * ? ")
    @Async
    public void test1() {
        for (int i = 0; i < 20; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("ScheduleThree-----子线程1名:"+Thread.currentThread().getName());
        }
    }
    @Scheduled(cron = "0 45 * * * ? ")
    @Async
    public void test2() {
        for (int i = 0; i < 20; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("ScheduleThree-----子线程2名:"+Thread.currentThread().getName());
        }
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值