1

Knowledge question = no code, no error, no examples to reports.

I applied a joinpoint over method() of ClassA, and ClassA is not a singleton. I create MyAspect that handle this joinpoint.

Question: if there are 2 callers of ClassA.method(), and callers works on 2 different threads (e.g. web requests ), AspectJ will run MyAspect over the 2 different threads or in a single one ?

I think that AspectJ execute MyAspect in the same thread of woven class (ClassA in this case).

tkruse
  • 10,222
  • 7
  • 53
  • 80
Giorgio Desideri
  • 169
  • 1
  • 12

1 Answers1

3

AspectJ will execute your aspects on the current thread. That means, if you have a multi-threaded execution, your aspect will run on multiple threads as well. If your aspect works with shared state, you'll need to make sure about accessing your shared state in a safe way.

Also note that there are other aspect instantiation models besides the default singleton. With a singleton aspect, only one instance of the aspect will be created in your app. You could use the perthis(), pertarget(), percflow() or percflowbelow() modifiers which changes how your aspect will get instantiated, and AspectJ will create a new instance of your aspect on a per-object or per-control-flow basis. You might be able to isolate aspect state with these instantiation models, but if you're using shared state, the basic rule still applies: you need to take care about thread-safe access to the shared state.

Nándor Előd Fekete
  • 6,988
  • 1
  • 22
  • 47