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