admin 管理员组文章数量: 887021
2024年1月18日发(作者:while循环例题)
2. public class AnnotationAsyncExample {3.
4. @Async5. public Future
1. @RunWith()2. @ContextConfiguration(classes = )3. public class AnnotationAsyncTest {4.
5. @Autowired6. AnnotationAsyncExample annotationAsyncExample;7.
8. @Test9. public void testWithParamWithReturn() throws Exception {0. for (int i = 0; i < 25; i++) {1. Future
6. n("hello, " + tThread().getName());7. }8. }测试:
1. @RunWith()2. @ContextConfiguration(classes = )3. public class AnnotationAsyncTest {4.
5. @Autowired6. AnnotationAsyncExample annotationAsyncExample;7.
8. @Test9. public void testExecuteWithMultiExecutor() {0. for (int i = 0; i < 25; i++) {1. eWithMultiExecutor();2. }3. }4. }结果:4.5 @Async的异常管理
2. public void voidParamVoidReturn() {3. n("hello, " + tThread().getName());4. int i = 1 / 0;5. }测试:
1. @Test2. public void testVoidParamVoidReturn() {3. for (int i = 0; i < 25; i++) {4. ramVoidReturn();5. }6. }结果:②AsyncConfigurerjavaConfig
1. @Configuration2. @ComponentScan3. @EnableAsync4. public class AnnotationConfig implements AsyncConfigurer {5.
6. @Override7. public Executor getAsyncExecutor() {8. ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
1. @Configuration2. @ComponentScan3. @ImportResource(locations = "classpath:spring/")4. public class SchedulerConfig/* implements AsyncConfigurer */{5.
6. }
1. 2.
8.
9.
0.
1. public static void main(String[] args) {2. ApplicationContext context = new AnnotationConfigApplicationContext();3. }结果:
版权声明:本文标题:Spring的任务执行器(TaskExecutor)和任务调度器(TaskScheduler) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1705579525h490832.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论