Spring @Async and sync

I have a Spring based asynchronous method (annotated with @Async ) that I want to pause in case the error event occurs a certain number of times. Since there may be more than one thread doing the same, I tried the static AtomicInteger ( MY_COUNT ) so that all threads can know about the account and use the built-in concurrency AtomicInteger .

In my unit tests (run with SpringJUnit4ClassRunner in Eclipse) everything will be fine until the stream reaches MY_COUNT.incrementAndGet() . Then the stream simply disappears. No exception, nothing. Asynchronous workflow just disappears. I tried to take out AtomicInteger and just use synchronized methods, but the same thing happens.

Question: Is there any kind of interaction between @Async and synchronization? Is it impossible to combine the two?


EDIT: more info: it looks like this has something to do with the scope of synchronization (if that's the correct term). As soon as I removed the static notation from the counter variable, it still bombed; but then when I changed it to Integer and moved the increment code to my own synchronized method, then the code will continue. I did not debug the Spring base code; Are there any Spring experts who could shed light on this behavior?

+6
source share
1 answer

No, async should not affect the behavior of an atomic whole.

With the information provided, the only logical conclusion is that there is some kind of exception that is not reported.

if possible for you, try installing the Uncaught exception handler

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.UncaughtExceptionHandler.html

In the handler, make sure that you trace the trace at least.

If this does not give you a solution or if you have a problem setting up an uncaught exception handler, send your code so that we can help you.

+4
source

Source: https://habr.com/ru/post/900300/


All Articles