admin 管理员组

文章数量: 887021


2024年1月18日发(作者:while循环例题)

2. public class AnnotationAsyncExample {3.

4. @Async5. public Future withParamWithReturn(String msg) throws Exception {6. Callable callable = () -> msg + " " + tThread().getName();7. FutureTask futureTask = new FutureTask<>(callable);8. new Thread(futureTask).start();9. return futureTask;0. }1. }测试:

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 future = ramWithReturn("msg" + i);2. n(());3. }4. }5. }

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. 7.

8.

9.

0. -->1. 2. 3. 4. 5. 6. 测试:

1. public static void main(String[] args) {2. ApplicationContext context = new AnnotationConfigApplicationContext();3. }结果:


本文标签: 循环 没有 例题 指定 作者