1

I would like to know if there is a way in AspectJ to declare an aspect per each thread, and also if there is a way to declare an aspect per team of threads?

By team of threads I am referring to this example:

A given thread 'X' creates 'N' new threads, then 'X' is the master of this team, if a given thread 'Y' within this team also creates by them self a new team, 'Y' will became the master of this new team. So I would like to know if I can declare an aspect 'A' to the team produce by the thread 'X' and a different "instance" of the aspect 'A' for the team 'Y'.

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
  • 1
    Think AspectJ doesn't has this ability. By the way, you can use ThreadLocal in your Aspect to create the state per thread. – Taky Feb 05 '13 at 09:39
  • @Taky Thanks for replying, nice workaround using the ThreadLocal. Yes, I am afraid aspectj indeed do not have this ability at least I did not manage to find anything yet. – dreamcrash Feb 05 '13 at 09:50

1 Answers1

1

AspectJ manipulates only byte code. So it cannot determine the runtime information(you know thread where code is invoked is runtime information) when generating Aspects hook.

If you need to create thread unique state use ThreadLocal. If you need to create ThreadGroup unique state you should do some manual work. See how it can be implemented: ThreadGroup local variables. Also be aware in Java you cannot determine parent thread, so you need to use ThreadGroup.

And every time when you want to use ThreadLocal state and Aspects, think about your program design, is it right way for you?

Community
  • 1
  • 1
Taky
  • 5,284
  • 1
  • 20
  • 29