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());
}
}
}

总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论