当前位置: 代码网 > it编程>编程语言>Java > Java之@Schedule与@Async注解失效问题总结

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

2026年05月09日 Java 我要评论
schedule注解失效schedule标记的方法的类没有被spring托管当@scheduled方法所属的类没有使用@component,@service,@controller等修饰的时候定时任务

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)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com