I have this method in Service class:
@Service
public class Service {
@Async
public synchronized void asyncTest() {
try {
System.out.println("entered");
Thread.sleep(5000);
System.out.println("exited");
System.out.println(this);
}
catch (InterruptedException e ) {}
}
}
When I call
@Test
public void asyncTest() throws InterruptedException {
service.asyncTest();
service.asyncTest();
Thread.sleep(10000);
}
it produces:
entered
entered
exited
exited
...Service@26d74d5f
...Service@26d74d5f
although I would expect
entered
exited
...Service@26d74d5f
entered
exited
...Service@26d74d5f
due to the synchronized keyword. Why?
My xml configuration:
<task:annotation-driven mode="aspectj" executor="fisExecutor" scheduler="fisScheduler"/>
<task:executor id="fisExecutor" pool-size="10" />
<task:scheduler id="fisScheduler" pool-size="30" />
<task:executor keep-alive="60" id="cmsExecutor" pool-size="5-00" queue-capacity="0" rejection-policy="CALLER_RUNS"/>
Spring version: 4.1.9.RELEASE JDK 1.7
Test class annotations:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:/applicationContext-*.xml")